Reputation:
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...?
Upvotes: 0
Views: 74
Reputation: 1789
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.
Upvotes: 0
Reputation: 1996
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:
Upvotes: 1