Reputation: 23
I want to create a function to check 3 values.
If the 3 values are in the range 50..99, I return true
, otherwise false
.
In this example, normally the answer is false
.
console.log(check_three_nums(65, 9, 199));
I have as answer true
.
I don't understand, why?
function check_three_nums(x, y, z) {
if ((x >= 50 && x <= 99) || (y >= 50 && y <= 99) || (z >= 50 && z <= 99)) {
return true;
} else {
return false;
}
}
// console.log(check_three_nums(50, 90, 99));
// console.log(check_three_nums(5, 9, 199));
// console.log(check_three_nums(65, 89, 199));
console.log(check_three_nums(65, 9, 199));
Upvotes: 0
Views: 69
Reputation: 11
"or operator" (||) will output true if one of the parameter values is true (condition1 is true or condition2 is true). You can use the "and operator" (&&), this operator will only output true if both parameter values are true (condition1 and condition2 are true)
Upvotes: 0
Reputation: 152
The double pipe operator (||) means OR, your function is currently returning true if at least one of the param is in your range. Try to change it whith the '&&' operator It should work as expected
function check_three_nums(x, y, z) {
if ((x >= 50 && x <= 99) && (y >= 50 && y <= 99) && (z >= 50 && z <= 99)) {
return true;
} else {
return false;
}
}
// console.log(check_three_nums(50, 90, 99));
// console.log(check_three_nums(5, 9, 199));
// console.log(check_three_nums(65, 89, 199));
console.log(check_three_nums(65, 9, 199));
Upvotes: 1
Reputation: 501
You can use this function to check any amount of number.
function check(...args) {
for (let a of args) {
if (a < 50 || a > 99)
return false
}
return true
}
Upvotes: 3