如何检测七巧板

时间:2018-11-28 03:31:28

标签: javascript pangram

七巧板是一个句子,其中每个字母至少包含一次。

到目前为止,这是我的代码:

const isPangram = (string) => {
    let alpha = string.toUpperCase().split("");
    for (let beta = 65; beta < 65 + alpha.length; beta++) {
        let gamma = String.fromCharCode(beta);
        if (alpha.includes(gamma)) {
            continue;
        }
        else {
            return false;
        }
    }
    return true;
}

console.log(isPangram("Detect Pangram"));

为什么“检测Pangram”返回true?

7 个答案:

答案 0 :(得分:1)

您可以使用.every这样非常简单的方法,如下所示。

alphabets = 'abcdefghijklmnopqrstuvwxyz'.split("");

const isPangram = (string) => {
    string = string.toLowerCase();
    return alphabets.every(x => string.includes(x));
}

console.log(isPangram("Detect Pangram"));
console.log(isPangram("abcd efgh ijkl mnop qrst uvwx yz"));

您可以从下面的链接中进一步了解every

  1. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/every
  2. https://www.w3schools.com/jsref/jsref_every.asp

答案 1 :(得分:1)

tokenizer.fit_on_texts("The earth is an awesome place live")

}

https://jsfiddle.net/xjys9dat/-相同的工作示例

您的错误是您正在运行循环,直到字符串的长度不是A-Z。

希望这会有所帮助。

答案 2 :(得分:1)

您的方法是遍历字母并验证目标字符串中是否存在每个字母a-z。但是,除非输入字符串的长度为26,否则循环条件65 + alpha.length不会覆盖字母表的大小。从65到91(包括65和91)的迭代将解决此问题。

这是另一种解决方案:

const isPangram = s =>
  new Set(s.toUpperCase().split``
   .filter(e => /[A-Z]/.test(e)))
   .size === 26
;

[
  "Detect Pangram",
  "abcd efgh ijkl mnop qrst uvwx yz",
  "abcd efgh ijkl mnop qrst uvwx y",
  "bcd efgh ijkl mnop qrst uvwx yz",
  "abcdefghijklmnopqrstuvwxyy",
  "AbCdEfGhIjKlM zYxWvUtSrQpOn",
  "How quickly daft jumping zebras vex."
].forEach(test => console.log(`${isPangram(test)}\t${test}`));

答案 3 :(得分:0)

另一种方法:

const alphabets26 = 'abcdefghijklmnopqrstuvwxyz';
let input = prompt();
input = input.toLowerCase();
let icount = 0;

for (let i = 0; i < alphabets26.length; i++) {
  var letter = alphabets26[i];
  if (input.indexOf(letter) > -1)
    icount++;
}

if (icount == 26)
  alert('All letters found at least once');
else
  alert('Few letters missing');

答案 4 :(得分:0)

正则表达式方法:

function isPangram(str){
    var regex = /([a-z])(?!.*\1)/gi;
    return (str.match(regex) || []).length === 26;
}

console.log(isPangram('abcdEfgHijklmnOpqrStuvwxyZ943')); //true
console.log(isPangram('hello 049 ds')); //false

参考:Javascript Pangram Regex

答案 5 :(得分:0)

const isPangram = (string) => {
    let alpha = string.toUpperCase().split("");

    // corecteded the bounding condition in the loop
    for (let beta = 65; beta < 91; beta++) {
        
        let gamma = String.fromCharCode(beta);
        if (alpha.includes(gamma)) {
            continue;
        }
        else {
            return false;
        }
    }
    return true;
}

console.log(isPangram("Detect Pangram"));
console.log(isPangram("abcd efgh ijkl mnop qrst uvwx yz"));
console.log(isPangram("abcd efgh ijkl mnop qrst uvwx y"));
console.log(isPangram("bcd efgh ijkl mnop qrst uvwx yz"));

答案 6 :(得分:0)

php 中的一个方法是这样的:

function detect_pangram($input) { 
    $alphabet = array('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');
    $isPangram = false;
    $array = str_split($input);
    foreach ($array as $char) {
    if (ctype_alpha($char)) {
    if (ctype_upper($char)) {
    $char = strtolower($char);
    }
    $key = array_search($char, $alphabet);
    if ($key !== false) {
    unset($alphabet[$key]);
    }
    }
    }
    if (!$alphabet) {
    $isPangram = true;
    }
    return $isPangram;
  }