Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to detect a pangram

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?

like image 499
hbagley Avatar asked Dec 08 '22 13:12

hbagley


1 Answers

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
like image 85
Karan Avatar answered Dec 10 '22 03:12

Karan