I have a doubt on an assignment of which I have the answer. The answer is little bit different from the code I've tried, so I'd like to clarify this doubt.
The statement is the following:
Make a function that receives a string as a parameter, mixing letters and numbers, and returns a string containing only letters of the past string at the same order.
Example: if the input is "ab2c4d", the function must return "abcd".
Example: if the input is "1234", the function must return "".
The correct answer is:
function removesNumbers(string) {
let newstring = "";
for (let i = 0; i<string.length; i++){
if (string[i] != "0" && string[i] != "1" && string[i] != "2" && string[i] != "3" && string[i] != "4" && string[i] != "5" && string[i] != "6" && string[i] != "7" && string[i] != "8" && string[i] != "9"){
newstring = newstring + string[i];
}
}
return newstring;
}
The code I've tried is almost the same, the only difference is that in the "if" statemente, instead of using &&, I used || and didn't get the correct output.
Could someone please explain why using || is the wrong way to go?
Thanks.
What I have tried:
function removesNumbers(string) {
let newstring = "";
for (let i = 0; i<string.length; i++){
if (string[i] != "0" || string[i] != "1" || string[i] != "2" || string[i] != "3" || string[i] != "4" || string[i] != "5" || string[i] != "6" || string[i] != "7" || string[i] != "8" || string[i] != "9"){
newstring = newstring + string[i];
}
}
return newstring;
}