最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to detect a pangram - Stack Overflow

programmeradmin2浏览0评论

A pangram is a sentence that contains every single letter of the alphabet at least once.

Here is my code so far:

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

Why does "Detect Pangram" return true?

A pangram is a sentence that contains every single letter of the alphabet at least once.

Here is my code so far:

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

Why does "Detect Pangram" return true?

Share Improve this question edited Nov 28, 2018 at 3:52 hbagley asked Nov 28, 2018 at 3:31 hbagleyhbagley 631 gold badge2 silver badges6 bronze badges 8
  • I think the condition in your loop is wrong. 65 is the starting value for beta, and is already definitely greater than the length of alpha. Shouldn't beta range from 65 to 90 ('Z')? – user47589 Commented Nov 28, 2018 at 3:34
  • 1 There's a two-line regex-based function over here: stackoverflow.com/a/32557405/74757 – Cᴏʀʏ Commented Nov 28, 2018 at 3:35
  • 1 Your loop should be more like: for (let beta = 65; beta < 91; beta++) { – Mark Commented Nov 28, 2018 at 3:36
  • To add to what Amy said, your loop should be from "A" to "Z", shouldn't it? not 65 to the length of the string? – Cᴏʀʏ Commented Nov 28, 2018 at 3:36
  • 1 @MarkMeyer You're right! I somehow missed the line before the for loop. My bad. – fardjad Commented Nov 28, 2018 at 3:52
 |  Show 3 more comments

8 Answers 8

Reset to default 5

You can do that very simple way with .every as shown below.

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

You can learn more about every from below links.

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

Your approach is to iterate through the alphabet and verify that every letter a-z is present in the target string. However, your loop conditional, 65 + alpha.length, won't cover the size of the alphabet unless the input string length is 26. Iterating from 65 to 91 inclusive will fix the problem.

Here's another solution which extracts alphabet characters, puts them into a set and checks that the set size is 26.

const isPangram = s => new Set(s.toUpperCase().match(/[A-Z]/g)).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}`));

Your mistake is that you were running the loop till the length of the string not A-Z.

Hope this helps.

const isPangram = (string) => {
  let alpha = string.toUpperCase().split("");
  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"));

https://jsfiddle.net/xjys9dat/ - A working example of the same

A regex approach:

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

Reference: Javascript Pangram Regex

An alternative approach:

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

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

An approach in php is like this:

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;
  }

If you know the input string is strictly letters only

const isPangram = sentence => new Set(sentence).size === 26;
console.log(isPangram("Detect Pangram"));
console.log(isPangram("abcd efgh ijkl mnop qrst uvwx yz"));
发布评论

评论列表(0)

  1. 暂无评论