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

标签 javascript arrays indexof

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

相关文章:

c# - 文本框 Indexof 和 LastIndexOf

javascript - Highchart 实时服务器示例使用大量 CPU

javascript - 处理ajax请求的流程控制(即仅在上一个请求完成后处理下一个请求)

c# - 如何创建带有标签而不是整数的数组

ios - 在 Swift 中使用 "Map"创建两个数组的超集

javascript - array.indexOf 只是 -1

JavaScript 向后迭代项目

javascript - Dojo:设置用于输入的默认文本框

arrays - 如何循环结构体数组并显示所有键值

arrays - UIPageControl 未正确更新为 UIPageViewController 中当前页面的索引