1

I'm trying to make a beginner program that returns true if an inputted string ends with a vowel and false if not, but am having issues given endsWith() only allows to do one letter at a time. messing around with if else options today didn't help me much and after a couple hours on one problem i'm ready for some help lol

here's what i have so far:

console.log(x.endsWith("e"));
console.log(x.endsWith("i"));
console.log(x.endsWith("o"));
console.log(x.endsWith("u"));```

any help is appreciated thanks so much. we're supposed to have just one boolean value show up and I'm stumped

Nguyễn Văn Phong
  • 13,506
  • 17
  • 39
  • 56
AstralV
  • 119
  • 7

5 Answers5

0

Just iterate through the vowels:

function endsVowel(str){
    for (let i of "aeiou"){
        if (str.endsWith(i)){
            return true;
        }
    }
    return false;
}
knosmos
  • 636
  • 6
  • 17
0

But am having issues given endsWith() only allows to do one letter at a time

So you should check the last char belongs to vowels - 'u', 'e', 'o', 'a', 'i' or not in this way.

const vowels = ['u', 'e', 'o', 'a', 'i'];

const isVowelAtLastCharacter = (str) => {
  const lastChar = str.charAt(str.length - 1);
  return vowels.includes(lastChar);
}

console.log(isVowelAtLastCharacter("xu"));
console.log(isVowelAtLastCharacter("xe"));
console.log(isVowelAtLastCharacter("xo"));
console.log(isVowelAtLastCharacter("xa"));
console.log(isVowelAtLastCharacter("xi"));

console.log(isVowelAtLastCharacter("xz"));
Nguyễn Văn Phong
  • 13,506
  • 17
  • 39
  • 56
0
const isEndsWithVowel=(s)=>{ 
    const vowelSet= new Set(['a','e','i','o','u']);

    return vowelSet.has(s[s.length-1]);
}
Joundill
  • 6,828
  • 12
  • 36
  • 50
  • 5
    While this code may provide a solution to the question, it's better to add context as to why/how it works. This can help future users learn and apply that knowledge to their own code. You are also likely to have positive-feedback/upvotes from users, when the code is explained. – Nguyễn Văn Phong Feb 03 '21 at 02:12
  • it is using set as hash map to unique elements and check in O(1) time. For this case, using array is good too but for bigger cases, set or map data structure has lower complexity – Tae Yeong Han Feb 12 '21 at 14:55
0

you can follow this code, I hope can help you, after review of Phong

let word_to_review = "California";

function reverseArray(arr) {
  var newArray = [];
  for (var i = arr.length - 1; i >= 0; i--) {
    newArray.push(arr[i]);
  }
  return newArray;
}

const getLastItem = reverseArray(word_to_review)[0];

let isVowel;
if (
  getLastItem === "a" ||
  getLastItem === "e" ||
  getLastItem === "i" ||
  getLastItem === "o" ||
  getLastItem === "u"
) {
  isVowel = true;      
} else {
  isVowel = false;
  
}

console.log(
  "is the last letter is vowel, yes or no ? The answer is.... " + isVowel
);
Luis Reales
  • 365
  • 3
  • 7
0

Another possible solution -

let x = "India"
const vowels = ['a','e','i','o','u']; //add capital letters too if string has capital letters

if(vowels.includes(x[x.length-1])){
   console.log('string ends with vowel', x);
}
Sachin Vairagi
  • 4,894
  • 4
  • 35
  • 61