codeintech3
codeintech3

Reputation: 67

Displaying strings based upon certain alphabet letters

I am trying to understand why my code isn't working. I'm creating a function that takes in a letter of the alphabet. If the letter is a, e, i, o or u then my program should display a message indicating that the entered letter is a vowel. If the letter is y then my function should display a message indicating that sometimes y is a vowel, and sometimes y is a consonant. Otherwise my function should display a message indicating that the letter is a consonant.

Here's my code

function vowelOrCons(letter) {
  if((letter) === 'a'; 'e'; 'i'; 'o'; 'u') {
    return 'vowel';
  } else if {
   ((letter) === 'y') {
     return 'vowel or consonant';
  } else if {
   ((letter) === "b", "c", "d", "f", "g", "h", "j", "k", "l", 
    "m", "n", "p", "q", "r", "s", "t", "v", "w", "x", "z")
     return 'consonant';
    }
    else
    {
     return 'Your entry is invalid';
    }
  }
}

I'm trying to learn Javascript better so I appreciate any help, resources, and/or explanations regarding my work, thanks!

Upvotes: 1

Views: 161

Answers (4)

Orel Eraki
Orel Eraki

Reputation: 12196

You problem is syntax.

if ((condition || another-condition) && condition) {
}
else if (some-other-condition) {
}
else {
}

Now concerning the checking, it can be shorten by using includes string method.

const firstLetter = letter.charAt(0);
if ("aeiou".includes(firstLetter)) { .. }

Upvotes: 2

CAllen
CAllen

Reputation: 836

I would suggest you declare an array for vowels, one for consonants and then check if the letter intered is in the array but first check if it is a 'y' as shown below;

var vowels  = ['a', 'e', 'i',....]];
var consonants  = ['b', 'c', 'd', ....]];

if ('y' == letter) {
    // print your sentence
}
else if (/* letter is in the vowels array */) {
    // print your sentence
}
else if (/* letter is in the consonants array */) {
    // print your sentence
}

Good luck

Upvotes: 1

Nina Scholz
Nina Scholz

Reputation: 386570

You could use String#includes and return if true with the message.

This code features the early exit approach by taking an if clause and return. The advantage is to omit else parts because the return exits the function.

function vowelOrCons(letter) {
    letter = letter.toLowerCase();
    if ('aeiou'.includes(letter)) {
        return 'vowel';
    }
    if (letter === 'y') {
        return 'vowel or consonant';
    }
    if ('bcdfghjklmnpqrstvwxz'.includes(letter)) {
        return 'consonant';
    }
    return 'Your entry is invalid';
}

console.log(vowelOrCons('a'));
console.log(vowelOrCons('b'));
console.log(vowelOrCons('1'));

Upvotes: 2

Bibberty
Bibberty

Reputation: 4768

Ok this need a bit of work. Take a look at the fixed code below. First off we move the test letters to arrays and used index of. Then we fixed syntax issues and added test log at end.

  function vowelOrCons(letter) {
     let vowels = ['a', 'e', 'i', 'o', 'u'];
     let consonants = ["b", "c", "d", "f", "g", "h", "j", "k", "l", 
          "m", "n", "p", "q", "r", "s", "t", "v", "w", "x", "z"];
     if(vowels.indexOf(letter) > -1 ) {
       return 'vowel';
     } else if(letter === 'y') {
          return 'vowel or consonant';
    } else if(consonants.indexOf(letter) > -1 ){
           return 'consonant';
      } else {
         return 'Your entry is invalid';
      }

   }

console.log(vowelOrCons('a'));
console.log(vowelOrCons(''));
console.log(vowelOrCons('1'));
console.log(vowelOrCons('y'));
console.log(vowelOrCons('w'));

Upvotes: 1

Related Questions