[ad_1]
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.
私が試したこと:
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; }
解決策 1
‘9’ の 16 進値は 0x39 で、それより上のすべては文字または特殊文字です。 つまり、’;:<=>?@’ ではありません。
特殊文字について心配していないように見えるので、心配していないと仮定します。
C++
if (string[i] >= 0x30) newstring = newstring + string[i];
[ad_2]
コメント