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.
string.charAt(i) == "a" || "A"should bestring.charAt(i) == "a" || string.charAt(i) == "A"(etc.), but there are much better ways of doing this.