Reputation: 139
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
Upvotes: 1
Views: 1783
Reputation: 5334
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);
}
Upvotes: 0
Reputation: 455
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
);
Upvotes: 0
Reputation: 1
const isEndsWithVowel=(s)=>{
const vowelSet= new Set(['a','e','i','o','u']);
return vowelSet.has(s[s.length-1]);
}
Upvotes: 0
Reputation: 14218
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"));
Upvotes: 0
Reputation: 626
Just iterate through the vowels:
function endsVowel(str){
for (let i of "aeiou"){
if (str.endsWith(i)){
return true;
}
}
return false;
}
Upvotes: 0