Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/372.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 为什么代码中的var计数增加??但是我提到了if(元音.indexOf(item!=-1))_Javascript_Arrays_Indexof - Fatal编程技术网

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

Javascript 为什么代码中的var计数增加??但是我提到了if(元音.indexOf(item!=-1)),javascript,arrays,indexof,Javascript,Arrays,Indexof,为什么代码中的var计数增加??但是我提到了if(元音.indexOf(item!=-1)) document.write(“”); 函数func7solve(str) { var元音=“aeiouAEIOU”; var arr=str.split(“”); var计数=0; arr.forEach(功能(项目) { 文件。写入(项目+“\n”); document.write(元音.indexOf(item)+“\n”); 文件。写入(计数+“\n”); if(元音索引of(项!=-1))

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

document.write(“”);
函数func7solve(str)
{
var元音=“aeiouAEIOU”;
var arr=str.split(“”);
var计数=0;
arr.forEach(功能(项目)
{
文件。写入(项目+“\n”);
document.write(元音.indexOf(item)+“\n”);
文件。写入(计数+“\n”);
if(元音索引of(项!=-1))
{
计数++;
}
});
返回计数;
}
写(func7solve(“快速棕色狐狸”);
文件。写(“”)应该是

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>');
否则,
元音.indexOf(item!=-1)
表示元音。indexOf(true)
表示
-1
-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>');
否则,
元音.indexOf(item!=-1)

表示
元音。indexOf(true)
表示
-1
-1
实际上是真实的,所以这就是计数增加的原因。

您错过了函数
indexOf
的使用。您需要正确使用该函数

试试这个:

if(元音索引of(项目)!=-1)

document.write(“”);
函数func7solve(str)
{
var元音=“aeiouAEIOU”;
var arr=str.split(“”);
var计数=0;
arr.forEach(功能(项目){
文件。写入(项目+“\n”);
document.write(元音.indexOf(item)+“\n”);
文件。写入(计数+“\n”);
if(元音索引of(项目)!=-1){
计数++;
}
});
返回计数;
}
写(func7solve(“快速棕色狐狸”);
文件。写(“”);

您错过了函数
indexOf
的使用。您需要正确使用该函数

试试这个:

if(元音索引of(项目)!=-1)

document.write(“”);
函数func7solve(str)
{
var元音=“aeiouAEIOU”;
var arr=str.split(“”);
var计数=0;
arr.forEach(功能(项目){
文件。写入(项目+“\n”);
document.write(元音.indexOf(item)+“\n”);
文件。写入(计数+“\n”);
if(元音索引of(项目)!=-1){
计数++;
}
});
返回计数;
}
写(func7solve(“快速棕色狐狸”);
文件。写(“”);

元音.indexOf(项目!=-1)
~>
元音.indexOf(项目)!=-1
我投票结束这个问题,因为它是由一个无法再复制的问题或一个简单的印刷错误引起的。虽然这里可能有类似的问题,但这一问题的解决方式不太可能对未来的读者有所帮助。
元音.indexOf(item!=-1)
~>
元音.indexOf(item)!=-1
我投票结束这个问题,因为它是由一个无法再复制的问题或一个简单的印刷错误引起的。虽然类似的问题可能在这里的主题,这是一个解决的方式不太可能帮助未来的读者。