为什么代码中的 var 计数增加了?不过我提到了 if(vowel.indexOf(item !== -1))。
document.write('<pre>');
function func7solve(str)
{
var vowel= "aeiouAEIOU";
var arr = str.split("");
var count = 0;
arr.forEach(function(item)
{
document.write(item+"\n");
document.write(vowel.indexOf(item)+"\n");
document.write(count+"\n");
if(vowel.indexOf(item !== -1))
{
count++;
}
});
return count;
}
document.write(func7solve("The quick brown fox"));
document.write('</pre>');
最佳答案
应该是
if(vowel.indexOf(item) !== -1)
否则,vowel.indexOf(item !== -1)
意味着 vowel.indexOf(true)
,它意味着 -1
并且 -1
实际上是 true,所以这就是计数增加的原因。
关于javascript - 为什么代码中的 var 计数增加?但是我提到了 if(vowel.indexOf(item !== -1)),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36714914/