gpt4 book ai didi

javascript - 为什么代码中的 var 计数增加?但是我提到了 if(vowel.indexOf(item !== -1))

转载 作者:行者123 更新时间:2023-12-02 14:44:33 24 4
gpt4 key购买 nike

为什么代码中的 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com