0

I have a problem running my code, which should count vowels in string and then return them in array. I cant't find the mistake(s) in my code. Thank you for your help.

<!DOCTYPE html>
<html>
<head>
<title></title>
<meta charset="utf-8">
<script>
function vowels(a) {
var a = "How are you today";
var numA = 0;
var numE = 0;
var numI = 0;
var numO = 0;
var numU = 0;
var i;

for (i = 0; i <= a.length; i++){

    if (string.charAt(i) == "a" || "A") {
        var numA += 1;
    }
    if (string.charAt(i) == "e" || "E") {
        var numE += 1;
    }
    if (string.charAt(i) == "i" || "I") {
        var numI += 1;
    }
    if (string.charAt(i) == "o" || "O") {
        var numO += 1;
    }
    if (string.charAt(i) == "u" || "U") {
        var numU += 1;
    }
    }
    str = [numA, numE, numI, numO, numU];
}
</script>
</head>
<body>
    return(str);
</body>
</html>
2
  • 1
    string.charAt(i) == "a" || "A" should be string.charAt(i) == "a" || string.charAt(i) == "A" (etc.), but there are much better ways of doing this. Commented Jan 12, 2018 at 14:53
  • (isn't Y a vowel ?) Commented Jan 12, 2018 at 15:04

3 Answers 3

2

While there is many better ways of doing it and all the other answers will give you some nice short method I think, given the current issues in your code, that you might find the shorthand methods confusing. I'm going to stick with your original code and try to fix that instead.

Though you should definitely look at the shorthand answers and if you feel comfortable with them accept one of them as an answer.

  • You are assigning a string value to a but yet call charAt() on string.
  • You are declaring your variables, i,e: var numA at the start but then re-declare them each time you want to increment their value.
  • You are not using the correct syntax for comparing both conditions as you need to be explicit a.charAt(i) == "a" || a.charAt(i) == "A"

var a = "How are you today";
var numA = 0;
var numE = 0;
var numI = 0;
var numO = 0;
var numU = 0;
var i;

for (i = 0; i <= a.length; i++) {

  if (a.charAt(i) == "a" || a.charAt(i) == "A") {
    numA += 1;
  }
  if (a.charAt(i) == "e" || a.charAt(i) == "E") {
    numE += 1;
  }
  if (a.charAt(i) == "i" || a.charAt(i) == "I") {
    numI += 1;
  }
  if (a.charAt(i) == "o" || a.charAt(i) == "O") {
    numO += 1;
  }
  if (a.charAt(i) == "u" || a.charAt(i) == "U") {
    numU += 1;
  }
}

str = [numA, numE, numI, numO, numU];

console.log(str)

I cant't find the mistake(s) in my code

In regards to that your console should have shown you several errors to debug.

For example, when re-declaring your variables while trying to increment them i.e: var numA += 1; that should have given you an error similar to:

Uncaught SyntaxError: Unexpected token +=

When calling charAt() on string instead of a i.e: string.charAt(i) you should have seen an error similar to:

Uncaught ReferenceError: string is not defined

Specifying the wrong conditional check is slightly trickier as that is valid code, though would behave incorrectly and give you the wrong result, i.e: a.charAt(i) == "a" || "A" when using your original input string should have resulted in:

[18,1,0,3,1]

Instead of:

[2,1,0,3,1]

Anyway, I thought adding this might help you going forward.

Sign up to request clarification or add additional context in comments.

Comments

0

Just check if the character is a vowel, and if yes increase the value in a lookup object:

function countVowels(string){
  const vowels = {};
  for(const char of string){
    if("aeiou".includes(char)){
        vowels[char] = (vowels[char] || 0) + 1;
    }
  }
  return vowels;
}

So one can do:

const {a, e, i, o, u} = countVowels("aeeiiioooouuuuu");
console.log(a); //1

Comments

0

Try this, this is what you tried to do, but simplified. I can answer your questions if you have any.

You start by declaring your counter object and your string to search in.

Then you iterate over this string. for each letter, you check if it is in your counter object. If not, it should return undefined. To avoid unecessary code, you can just put them all in lower case.

If the letter is in the counter, simply add 1 to the counter.

After that, I just log the counter, it was faster than adding it to your HTML.

const counters = {
  a: 0,
  e: 0,
  i: 0,
  o: 0,
  u: 0,
  y: 0
};

const str = "How are you today";

for (const char of str) {
  if (counters[char] !== undefined) {
    counters[char.toLowerCase()]++;
  }
}

console.log(counters);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.