2
"use strict";

function vovelsAndConstants(s) {

  let vowels = new Array();
  let constants = new Array();

  for (let a = 0; a < s.length; a++) {
    //console.log(s[a]);
    if (s[a] === "a" || "e" || "i" || "o" || "u") {
      console.log(s[a]);
    }
  }
 
}

vovelsAndConstants("sosisvesalam");

I can't understand why or operator here doesn't work It all makes sense to me. It outputs all chars instead of vowels

5
  • s[a] === "a" || "e" ----> s[a] === "a" || s[a] === "e" ... Commented Jan 16, 2021 at 13:01
  • Use includes. Commented Jan 16, 2021 at 13:08
  • if (s[a] === "a" || "e" || "i" || "o" || "u") means if((s[a] === "a") || Boolean("e") || Boolean("i") || Boolean("o") || Boolean("u")) which is basically if((s[a] === "a") || true || true || true || true) or simply if(true). Take a look at Operator Precedence Commented Jan 16, 2021 at 13:10
  • off topic, but take a look at this: let vowels = s.match(/[aeiou]/gi); let consonants = s.match(/[^aeiou]/gi); or better let consonants = s.match(/(?=[a-z])[^aeiou]/gi); Commented Jan 16, 2021 at 13:22
  • Does this answer your question? Concise way to compare against multiple values Commented Jan 17, 2021 at 16:59

4 Answers 4

4

IMO the easiest way is to use includes, otherwise you will have to list all the different comparisons separately.

Replace:

s[a] === "a" || "e" || "i" || "o" || "u"

with:

["a","e","i","o","u"].includes(s[a])

or as Thomas suggested in his comment:

"aeiou".includes(s[a])

These will do a string comparison and not a type comparison as === would do but it could still work for you.

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

3 Comments

You don't need the array here (overhead). You can use String#includes "aeiou".includes(s[a])
@Thomas true! I guess an array would be more practical for the OP. But nice addition, thanks !
IMHO, this should be the accepted answer.
1

You need to check for equality for each case:

function vovelsAndConstants(s) {
  const vowels = new Array();
  const constants = new Array();
  for (let a = 0; a < s.length; a++) {
    if (s[a] === "a" || s[a] === "e" || s[a] === "i" || s[a] === "o" || s[a] === "u") {
      console.log(s[a]);
    }
  }
}

vovelsAndConstants("sosisvesalam");

Another way using .indexOf:

const VOWEL_LETTERS = ["a","e","i","o","u"];

function vovelsAndConstants(s) {
  const vowels = new Array();
  const constants = new Array();
  for (let a = 0; a < s.length; a++) {
    if (VOWEL_LETTERS.indexOf(s[a]) !== -1) {
      console.log(s[a]);
    }
  }
}

vovelsAndConstants("sosisvesalam");

Comments

1

It makes sense to you based on the language we speak, whereas with programming you have to be explicit.

if (s[a] === "a" || s[a] === "e" || s[a] === "i" || s[a] === "o" || s[a] === "u") {
   console.log(s[a]);
}

JavaScript doesn't know what you are comparing "e" or "i" or "o" to, you need to tell it every time.

Comments

1

Your current condition only compare the a character successfully. From the second check all comparison segment is true, hence outputs all the characters.

You have to compare the currently iterated character with all possible vowels in the condition:

"use strict";

function vovelsAndConstants(s) {

  let vowels = new Array();
  let constants = new Array();

  for (let a = 0; a < s.length; a++) {
    if (s[a] === "a" || s[a] ==="e" || s[a] ==="i" || s[a] ==="o" || s[a] ==="u") {
      console.log(s[a]);
    }
  }
 
}

vovelsAndConstants("sosisvesalam");

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.