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

Marios
  • 26,333
  • 8
  • 32
  • 52
  • `s[a] === "a" || "e"` ----> `s[a] === "a" || s[a] === "e"` ... – Yousaf Jan 16 '21 at 13:01
  • Use `includes`. – Marios Jan 16 '21 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](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Operator_Precedence) – Thomas Jan 16 '21 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);` – Thomas Jan 16 '21 at 13:22
  • Does this answer your question? [Concise way to compare against multiple values](https://stackoverflow.com/questions/13737091/concise-way-to-compare-against-multiple-values) – Sebastian Simon Jan 17 '21 at 16:59

4 Answers4

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.

Marios
  • 26,333
  • 8
  • 32
  • 52
  • 2
    You don't need the array here *(overhead)*. You can use [String#includes](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/includes) `"aeiou".includes(s[a])` – Thomas Jan 16 '21 at 13:18
  • @Thomas true! I guess an array would be more practical for the OP. But nice addition, thanks ! – Marios Jan 16 '21 at 13:20
  • 1
    IMHO, this should be the accepted answer. – AbsoluteBeginner Jan 16 '21 at 14:26
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");
Majed Badawi
  • 27,616
  • 4
  • 25
  • 48
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.

Dark Hippo
  • 1,255
  • 2
  • 15
  • 35
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");
Mamun
  • 66,969
  • 9
  • 47
  • 59