I was doing some algorithm challenge. I am taking a number, converting it to a string then to an array and checking each of its element one by one. Code below works on javascript. The goal is that, if all of the numbers are the ones in the if statement, then the function should return true. But if there is 7 or any other value, then the function will return false.
var a = myFunction(3);
if(a === true){
alert("true");
}
else{
alert("false");
}
function myFunction(number){
let array = number.toString().split("");
for(var i = 0; i<array.length; i++){
if(array[i] !== '0' && array[i] !== '1' && array[i] !== '8' && array[i] !== '5' && array[i] !== '2'){
return false;
}
}
return true;
}
I am stuck on the logic below.
if(array[i] !== '0' && array[i] !== '1' && array[i] !== '8' && array[i] !== '5' && array[i] !== '2')
Why will it not work right if I change '&&' to '||'? I am confused by the fact, if array[i] = 1, then it should be false because array[i] !== 0 or 8 or other numbers on the if statement.
alert(a === true)(a != 1 || a != 2), if a is 1 then the "not equal to 2" part is true and the whole thing is true, even though the a != 1 part is false.