javascript 迭代应该在“继续”之后结束时如何中断?

eoxn13cs  于 2022-12-25  发布在  Java
关注(0)|答案(4)|浏览(116)

所以我正在做一些关于代码战的挑战,并陷入了以下问题:

  • 编写一个函数来检查字符串是否包含字母表中的所有字母,无论是大写还是小写。

下面是我的代码:

function isPangram(string) {
  let alphabet = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"]

  for (let i = 0; i < 26; i++) {
    if (string.toLowerCase().includes(alphabet[i])) {
      continue;
    } else {
      return false
    }
    return true
  }
}

console.log(isPangram("The quick brown fox jumps over the lazy dog"))

然而,"continue"并不像我想的那样工作,因为在'alphabet [26]'的时候,它会转到else-block并返回false。显然,我不能返回true而不是continue,因为那样的话,只要字符串包含任何字母,它总是会返回true。我想的另一件事是,如果你甚至需要for循环。从逻辑上讲,如果我可以写

if (string.toLowerCase().includes(alphabet[0-25])){"

但那也不行。

643ylb08

643ylb081#

这是一个简单一点:

const alphabet = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"]

function isPangram(input_string) {
  let string = input_string.toLowerCase()
  for (let i = 0; i < 26; i++) {
    if (!string.includes(alphabet[i])) {
      return false
    }
  }
  return true
}

console.log(isPangram("The quick brown fox jumps over the lazy dog"))

当检查“not true”就可以了,并且您的return true语句位于for循环中时,不需要continue,这将导致isPangram("a")计算为true

ztmd8pv5

ztmd8pv52#

其他的回答和评论显示了你的错误。
以下是使用every的较短版本

let alphabet = [..."abcdefghijklmnopqrstuvwxyz"]; // spread makes an array

const isPangram = string => {
  const str = string.toLowerCase();
  return alphabet.every(letter => str.includes(letter)); // every returns as soon as something is false
};

console.log(isPangram("The quick brown fox jumps over the lazy dog"))
console.log(isPangram("The quick brown foe jumps over the lazy dog"))

使用Set

const isPangram = string =>  {
  const chars = [...string.toLowerCase()]; // Convert to an array of lowercase letters
  const letters = new Set(); // a set stores unique values
  chars.forEach(char => (char >= 'a' && char <= 'z') && letters.add(char));
  return letters.size === 26; // number of letters in the English alphabet
};

console.log(isPangram('The quick brown fox jumps over the lazy dog')); // true
console.log(isPangram('This is not a pangram')); // false
console.log(isPangram('ABCDEFGHIJKLMNOPQRSTUVWXYZ')); // true
t3psigkw

t3psigkw3#

我想我可能会稍微简化一下...如果你使用“!”来反转includes测试返回的布尔值,你就不需要continue语句了。

function isPangram(string) {
  let alphabet = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"]

  for (let i = 0; i < 26; i++) {
    if (!string.toLowerCase().includes(alphabet[i])) {      
      return false
    }
  }
  return true
}

console.log(isPangram("The quick brown fox jumps over the lazy dog"))
sqxo8psd

sqxo8psd4#

只是做了一点小改动,为你提供的例子工作。

function isPangram(string) {
    let response = true;
  let alphabet = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"]

  for (let i = 0; i < 26;) {
    if (string.toLowerCase().includes(alphabet[i])) {
      i++;
    } else {
      response = false
      break;
    }
  }
  return response;
}

console.log(isPangram("The quick brown fox jumps over the lazzy dog"))
console.log(isPangram("The quick brown fox jumps over the lazzy d"))

相关问题