mardi 31 janvier 2017

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...?

Aucun commentaire:

Enregistrer un commentaire