OR Logic in if statement issue.

I am having a problem with the logic in my if statement. I am trying to have it check to see if the strings character is equal to a, e, i, o, OR u. And then if so add the character to the phrase string. Else add "x" to the phrase string.

The if statement seems to disregard my OR logic and returns true regardless if it is a vowel of not.

function translate(string){
    var phrase = "";

    for(i=0; i<string.length; i++){
        if (string.charAt(i) === "a" || "e" || "i" || "o" || "u"){
           phrase += string.charAt(i);

       }else{

            console.log("x");

         }
    }
    console.log(phrase);
}


translate("this is fun");

Any help would be greatly appreciated! Thank you.

Upvotes: 1

Views: 103

Answers (5)

RobG
RobG

Reputation: 147363

Alex's answer is pretty good, but rather than using indexOf and an array (noting that Array.prototype.indexOf is ES5 so not supported by older browsers), you can use an object instead:

var vowels = {a:'a', e:'e', i:'i', o:'o', u:'u'};

if (vowels.hasOwnProperty(string.charAt(i).toLowerCase())) {
    phrase += string.charAt(i);
} else {
    ...
}

The above is also not case sensitive, so A, E, I, O and U will also be added to the string. If you want it to be case sensitive, remove the .toLowerCase() part.

Edit

Alex got me thinking, again. To return an array of just the vowels in order:

function getVowels(s) {
  return s.match(/[aeiou]/g);
}

To return a string where all non-vowels (consonants) are replaced with an "x":

function replaceConsonants(s) {
  return s.replace(/[^aeiou]/g,'x');
}

To return a string of just the vowels:

function replaceConsonants(s) {
  return s.replace(/[^aeiou]/g,'');
}

or

function getVowels(s) {
  return s.match(/[aeiou]/g).join('');
}

etc.

Upvotes: 1

yowza
yowza

Reputation: 249

Do it like this. The .indexOf() on strings is more widely available than the .indexOf() on Arrays.

if ("aeiou".indexOf(string.charAt(i)) > -1) {

Upvotes: 1

alex
alex

Reputation: 490163

if (string.charAt(i) === "a" || "e" || "i" || "o" || "u"){

This is incorrect. If the first condition fails (the character is not "a"), it will always be truthy because it will evaluate "e", which is truthy (JavaScript returns the last evaluated part of an expression in a condition).

You could use...

// Define them somewhere out of the loop.
var vowels = ["a", "e", "i", "o", "u"];

// Your new condition.
if (vowels.indexOf(string.charAt(i)) > -1) {

You could also rewrite the whole thing like...

var vowels = ["a", "e", "i", "o", "u"];
var phrase = string
              .split("")
              .filter(function(char) { return vowels.indexOf(char) > -1; })
              .join("");

jsFiddle.

Upvotes: 5

David G
David G

Reputation: 96800

You need to check each condition separately. For example:

if (string.charAt(i) === "a" || string.charAt(i) === "e" || ...);

To cut down on code bloat you can set a variable:

var char = string.charAt(i);

if (char === "a" || char === "e" || ...);

Or you can use this indexOf trick:

if (["a", "e", "i", "o", "u"].indexOf(string.charAt(i)) > -1);

Upvotes: 2

Majid Laissi
Majid Laissi

Reputation: 19788

In you code you are comparing string.charAt(i) with "a" || "e" || "i" || "o" || "u" which evaluates to true.

What you men to do is :

string.charAt(i) === "a" || string.charAt(i) === "e" 
|| string.charAt(i) === "i" || string.charAt(i) === "o" || string.charAt(i) === "u"

In english we say : if my string is equal to 'a' or 'e' or 'i' .. but in javascript (and most other languages) we say : if my string is equal to 'a' or my string is equal to 'b' ..

Upvotes: 1

Related Questions