1

我正在尝试制作一个初学者程序,如果输入的字符串以元音结尾,则返回 true,如果不是,则返回 false,但是给定的问题 endsWith() 一次只允许执行一个字母。今天搞乱if else选项对我没有多大帮助,在解决一个问题几个小时后,我准备好寻求帮助了,哈哈

这是我到目前为止所拥有的:

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

非常感谢任何帮助。我们应该只显示一个布尔值,我很难过

4

4 回答 4

0

只需遍历元音:

function endsVowel(str){
    for (let i of "aeiou"){
        if (str.endsWith(i)){
            return true;
        }
    }
    return false;
}
于 2021-02-03T01:38:30.077 回答
0

但是我遇到的问题endsWith() 只允许一次写一封信

vowels - 'u', 'e', 'o', 'a', 'i'所以你应该以这种方式检查最后一个字符是否属于。

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"));

于 2021-02-03T01:40:21.267 回答
0
const isEndsWithVowel=(s)=>{ 
    const vowelSet= new Set(['a','e','i','o','u']);

    return vowelSet.has(s[s.length-1]);
}
于 2021-02-03T01:47:40.513 回答
0

您可以按照此代码进行操作,希望对您有所帮助,经过 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
);
于 2021-02-03T02:11:17.253 回答