0

Why the var count increasing in the code ?? however i mentioned 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>');
  • 3
    `vowel.indexOf(item !== -1)` ~~> `vowel.indexOf(item) !== -1` – Yury Tarabanko Apr 19 '16 at 09:56
  • I'm voting to close this question because it was caused by **a problem that can no longer be reproduced** or **a simple typographical error**. While similar questions may be on-topic here, this one was resolved in a manner unlikely to help future readers. – Zakaria Acharki Apr 19 '16 at 10:20

2 Answers2

1

it should be

if(vowel.indexOf(item) !== -1)

Otherwise, vowel.indexOf(item !== -1) means vowel.indexOf(true) and it means -1 and -1 is actually truthy, so that's why the count got incremented.

nonopolarity
  • 146,324
  • 131
  • 460
  • 740
1

You miss use of the function indexOf. You need to use the indexOf function properly.

try with this:

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>');
Murad Hasan
  • 9,565
  • 2
  • 21
  • 42