【问题标题】:Write a function definition named hasVowels that takes in value and returns true if the string contains any vowels编写一个名为 hasVowels 的函数定义,它接受值并在字符串包含任何元音时返回 true
【发布时间】:2023-02-07 11:33:59
【问题描述】:

我有一个关于 JavaScript 的入学考试。我是新手,被卡住了。我什至不知道如何解决这个问题。我的函数定义必须满足这个标准才能继续。

编写一个名为 hasVowels 的函数定义,它接受值并在字符串包含任何元音时返回 true。

assert(hasVowels("banana"), true, "Exercise 44");
assert(hasVowels("ubuntu"), true, "Exercise 44");
assert(hasVowels("QQQQ"), false, "Exercise 44");
assert(hasVowels("wyrd"), false, "Exercise 44");

function hasVowels(a,e,i,o,u) {
  return (a, e, i, o, u);
}

【问题讨论】:

标签: javascript


【解决方案1】:

尝试以下实现

assert(hasVowels("banana"), true, "Exercise 44");
assert(hasVowels("ubuntu"), true, "Exercise 44");
assert(hasVowels("QQQQ"), false, "Exercise 44");
assert(hasVowels("wyrd"), false, "Exercise 44");

function hasVowels(s) {
  const vowels = ['a', 'e', 'i', 'o', 'u']
  vowels.forEach((vowel) => {
    if(s.includes(vowel.toLoweCase()) || s.includes(vowel.toUpperCase())) return truel
  })
  return false;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-08-18
    • 2022-11-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多