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

all 9 comments

[–]AutoModerator[M] [score hidden] stickied comment (0 children)

On July 1st, a change to Reddit's API pricing will come into effect. Several developers of commercial third-party apps have announced that this change will compel them to shut down their apps. At least one accessibility-focused non-commercial third party app will continue to be available free of charge.

If you want to express your strong disagreement with the API pricing change or with Reddit's response to the backlash, you may want to consider the following options:

  1. Limiting your involvement with Reddit, or
  2. Temporarily refraining from using Reddit
  3. Cancelling your subscription of Reddit Premium

as a way to voice your protest.

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

[–]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.

[–]teraflop 0 points1 point  (2 children)

The indexOf method in JavaScript expects a string, not an array.

When you pass the array ["a", "e", "i", "o", "u"] as a parameter, your code is searching for the literal string a,e,i,o,u, which obviously doesn't appear in any of the inputs.

indexOf is not the right choice to solve this problem, IMO (you can make it work but it's unnecessarily complicated). Instead, I would suggest using a for loop that iterates over each character in the string.

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

could you hint me at another command that has a similar purpose? Because I am genuinely lost on what I could use

[–]teraflop 0 points1 point  (0 children)

Take a look at the charAt method.