Creating a vowel and consonant counter - not sure what isn't working

I’m trying to practice with loops and nested “if” statements inside of them, so I created a prompt box that asks for a sentence, then made a loop that runs through each character in the string and is supposed to count how many vowels and consonants. But, for some reason it just cycles through and counts each character as the first action in the “if/else” statement. In this case, I wrote vowels first so it is counting all characters as vowels. not sure where I went wrong, would love some feedback.

EDIT It wouldn’t allow me to post 2 images, but I have the results image in the thread.

1 Like

So here’s part of your problem (haven’t gone thru the logic, simply looking at syntax at this point): your if statements are completely bug-nuts crazy (at least, that’s what the JS interpreter will say).

if(letter === 'a' || 'e' || 'i' || 'o' || 'u' || 'y'){

What that line says is “if the variable letter is set to ‘a’, OR if ‘e’ or ‘i’ or ‘o’ or ‘u’ or ‘y’…”

In other words, you have one equality test (letter === ‘a’), then you simply check if ‘e’ evaluates as true (which it is) or ‘i’ is true-ish (which it is)…

In order to do these, you would have to compare letter to each vowel you want. A cleaner way to express this one might be a switch/case, but the if would be perfectly valid. Just bear in mind that building a complex expression is simply taking a few simple expressions and joining them with an and, or an or, or some sort of javascript “conjunction”. But each simple expression is still complete in itself:

if (letter === 'a' ||
    letter === /* next option check letter against */ ||
    letter === /* and so on, checking each vowel or consonant against letter  */){
1 Like

That worked! Thank you!!

Excellent! Glad it worked out. Now… for extra credit… wanna try the same thing with a ‘switch’ statement? lol

… wait, 26 chars but only twenty accounted for. Punctuation and spaces?

You can also consider using String#includes. Example:

'abcde'.includes('c'); // true
1 Like

Hi celestXrose how u r doin? In cour case the reason your code is not working is coz yo used || logical operator which starts from left hand side of the operator and if it evaluates to true it does not care at all about the rest of the code that comes after it it js engince executes next new line. this code might give u a little bit hint.
// Creating a vowel and consonant counter - not sure what isn't working
let vowels = 0
let constants = 0
let vowelChars = ‘auioey’
let constantsChars = ‘bcdfghjklmnpqrstvwxz’
let sentence = prompt(‘Enter a sentence’)
} else if(constantsChars.includes(l)){

console.log(Sentence length is ${sentence.length}There are ${vowels} vowels and ${constants} constants in sentence)
. Good luck to your studies :slight_smile:


yes, punctuation and spaces count for characters but not for vowels or consonants :slight_smile: I was introduced to the concept of a switch/case through the FCC curriculum but I branched off to follow some classes on SkillShare to get a better grasp of things I was struggling with on FCC. The teacher was demonstrating loops with an if/else statement that counted how many O’s were in a sentence, so I took that concept and made the vowel/consonant counter, but I didn’t have an example of an if/else statement with OR operators in front of me, which is where my hiccup came from :sweat_smile:
I will probably go back and refresh myself on the switch/case, then re-write the code, I was just stubbornly trying to make my dabbles operate how I wanted them to haha.


And it is a great thing to practice with complex expressions like that. They take some getting used to. As @Johongirmirzo mentioned, the way the logical OR (||) or the logical AND (&&) evaluate can take some getting used to.

In the case of the logical or statement, which might look like this:

if (userName.length < 8 || userName.toLowerCase().indexOf('john') !== -1 || password.length < 8 ){...}

… each piece is evaluated, left to right, until the first TRUE evaluation occurs. So if userName = john and password = foo|3bar, then the first simple expression, userName.length < 8, will return false, causing the next expression to be evaluated. In our case, userName.toLowerCase().indexOf('john') would NOT be equal to -1 (which says that ‘john’ is in our userName), and that expression would evaluate to true. When that happens, then the entire complex expression reads as true, and the if statement is processed.

In the case of a logical and statement, like

if ( userName.toLowerCase().indexOf('john') !== -1 && password.toLowerCase().indexOf('john') === -1 && password.length < 8 )

… it will also evaluate, left to right, each simple expression. This time, though, the statement will return false for the FIRST FALSE we encounter. If we again had userName = 'john' and `password = ‘13John47aC’, just to be funky, what would happen?

  1. userName.toLowerCase().indexOf('john') is NOT equal to -1, so that would be true. The complex expression would continue to test.
  2. password.toLowerCase().indexOf('john') **is NOT** equal to -1 (the wordjohnis in the password), so this would returnfalse`. As soon as the if statement encountered this, the entire expression is read as false. The if statement will not execute.
  3. In our case, the third option wouldn’t even be hit. At this point, it wouldn’t matter. True or false for that one, the entire expression has already read as false.

Note, too, that you can group them:

if (userName.length > 8 || password.length > 8 && Number(userAge) > 18 ) {...}

But the order of operations becomes a thing. The AND is tested before the OR, and if the AND evaluates as true, then the whole thing is true.

To read more: