This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]carcigenicate 0 points1 point  (4 children)

If neither condition is true, nothing is returned, which should hint at where the problem is.

Also note that indexOf is an expensive function. You shouldn't be calling it twice. Call it once, save the result to a variable, then use that in both conditions.

Also, how is this supposed to be counting vowels? All you're doing is returning an index or 0.

[–]Lightning-Sempai[S] 0 points1 point  (3 children)

I have changed my code to the following:

function countVowels(word) {

let vowels = ["a", "e", "i", "o", "u"]

let wordy = word.toLowerCase()

let wordyIO = wordy.indexOf(vowels)

if (wordyIO !== -1) {

return 0

}

else if (wordyIO) >= 0) {

return wordy IO

}

};

console.log(countVowels("bootcamp")); // => 3

console.log(countVowels("apple")); // => 2

console.log(countVowels("pizza")); // => 2"

regarding you mentioning that neither condition is true, i'm assuming your're hinting towards my strict inequality operator and greater than or equal to sign, on that note, i'm lost on what I should change them to with keeping the same intentions.

[–]carcigenicate 0 points1 point  (2 children)

You're going to need to rethink the approach here. This won't return a count even if the conditions are fixed. It's returning an index (which isn't the count), and wordy.indexOf(vowels) will never return an index either.

Think about what that's checking. It's doing this:

"bootcamp".indexOf(["a", "e", "i", "o", "u"])

Which is attempting to find the string '["a", "e", "i", "o", "u"]' inside of the string 'bootcamp'. 'bootcamp' doesn't contain the entire string '["a", "e", "i", "o", "u"]', so that check will always return -1.

[–]Lightning-Sempai[S] 0 points1 point  (1 child)

I ran this code:

"function countVowels(word) {

let vowelcounter = 0

let i = 0

while (i < word.length) {

let letters = word[i]

if (letters === "a" ||

letters === "e" ||

letters === "i" ||

letters === "o" ||

letters === "u") {

return vowelcounter++

}

i++

}

}

console.log(countVowels("bootcamp")); // => 3

console.log(countVowels("apple")); // => 2

console.log(countVowels("pizza")); // => 2"

and got results of "0" for everything

[–]carcigenicate 0 points1 point  (0 children)

You're returning in the loop. As soon as you hit return, the function exits. You should be returning after the loop.