简体   繁体   中英

Javascript Logical Operator Confusion

I have a complete working program, but I am very confused about the logic in a couple of my conditional statements after I fiddled with them in an attempt to get the program to do what I want.

    while (col < 5 || 20 < col || Number.isInteger(col)) {
        col = prompt("Columns (Int between 5 and 20): ","10");
        console.log("col " + col);
    }
    while (row < 5 || 20 < row || Number.isInteger(row)) {
        row = prompt("Rows (Int between 5 and 20): ","10");
        console.log("row " + row);
    }

    if (row>col) {size = 400/col;console.log("colmore");}
    else if (col>row) {size = 400/row;console.log("rowmore");}
    else {size = 400/col;console.log("same");}
    console.log("size " + size);

Now my program prompts for the number of Columns and then Rows. For example, I'll put in 20 for columns, and 5 for rows - columns are obviously more than rows then. So this happens:

col 20
row 5
colmore
size 20

Obviously that's what I want it to do, but I'm getting hung up because that first condition

if (row>col)

should mean if rows are more than columns, and the program should continue to the next statement... or am I just completely losing my mind...?

I would try something like this:

while (col < 5 || col > 20 || !Number.isInteger(col)) {
    col = Number(prompt("Columns (Int between 5 and 20): ","10"));
    console.log("col " + col);
}

while (row < 5 || row > 20 || !Number.isInteger(row)) {
    row = Number(prompt("Rows (Int between 5 and 20): ","10"));
    console.log("row " + row);
}

Changes:

  • Convert the string input to a number so we wrap the prompt response in a Number() call; this casts the string to a number returning either a number or NaN if it isn't a number
  • Stay in the while loop if it is NOT an integer (you had if it is an integer); notice the !
  • Switch the second condition in the while so it reads more naturally (perhaps a matter of taste but I find it easier to read col < 5 or col > 20 than col < 5 or 20 < col)

With if (row>col) you are saying if rows number(int) is greater than col number(int) then run this code

size = 400/col;console.log("colmore");

and skip this code

else if (col>row) {size = 400/row;console.log("rowmore");}
else {size = 400/col;console.log("same");}

and then run this code

console.log("size " + size);

I have edited your code and broken down the steps to help you.

//declare variable as prompt first
var col = prompt("Columns (Int between 5 and 20): ","10");
/* ** continue to display prompt if user enters a number below 5 or a number above 20 or a character which is not a number ** */
while (col < 5 || col < 20 || isNaN(col)) {
  col = prompt("Columns (Int between 5 and 20): ","10");
  console.log("col " + col);
}
// notice the use of isNaN(row) which is checking the col variable

//declare variable as prompt first
var row = prompt("Rows (Int between 5 and 20): ","10");
/* ** continue to display prompt if user enters a number below 5 or a number above 20 or a character which is not a number ** */
while (row < 5 || row < 20 || isNaN(row)) {
  row = prompt("Rows (Int between 5 and 20): ","10");
  console.log("row " + row);
}
// notice the use of isNaN(row) which is checking the row variable

//First check if row is greated than col
if (row > col) {
  size = 400/col;console.log("More Rows " + row + "rows and " + col + "cols");//if true run this code
}
//if row is not greater than col then check if col is greater than row
else if (col > row) {
  size = 400/row;console.log("More Cols " + col + "cols" + row + "rows and ");//if true then run this code
}
//else row and col must be equal
else {
  size = 400/col;console.log("same amount");//so run this code run this code
}
//end if statement and move on with code
console.log("size " + size);

Your definately on the right track. Would recommend using isNaN() method.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM