4

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?

hbagley
  • 63
  • 2
  • 6
  • 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')? –  Nov 28 '18 at 03:34
  • 1
    There's a two-line regex-based function over here: https://stackoverflow.com/a/32557405/74757 – Cᴏʀʏ Nov 28 '18 at 03:35
  • 1
    Your loop should be more like: `for (let beta = 65; beta < 91; beta++) {` – Mark Nov 28 '18 at 03: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ᴏʀʏ Nov 28 '18 at 03:36
  • Also, the length of the string is less than 26 characters. It isn't possible for it to contain every character of the alphabet at least once. –  Nov 28 '18 at 03:37
  • As noted by others, the for loop condition seems to be the problem. I think you wanted the condition to be something like `beta < 65 + alpha.length` – fardjad Nov 28 '18 at 03:45
  • @fardjad `alpha` is the passed in string, the naming makes it seem like it's the alphabet, but it's not. – Mark Nov 28 '18 at 03:47
  • 1
    @MarkMeyer You're right! I somehow missed the line before the for loop. My bad. – fardjad Nov 28 '18 at 03:52

8 Answers8

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
Karan
  • 12,059
  • 3
  • 24
  • 40
  • 2
    This is the best one in the list, however I would move `toLowerCase()` outside the `every` call, so that it isn't repeated for every letter in the alphabet every time. – Matt Way Nov 28 '18 at 05:17
  • 1
    @MattWay Thanks, That's a good suggestion. I've updated my code. – Karan Nov 28 '18 at 05:20
3

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

Karan
  • 12,059
  • 3
  • 24
  • 40
3

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}`));
ggorlen
  • 44,755
  • 7
  • 76
  • 106
2

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

icecub
  • 8,615
  • 6
  • 41
  • 70
1

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');
Karan
  • 12,059
  • 3
  • 24
  • 40
Axel
  • 4,365
  • 11
  • 63
  • 122
1

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"));
Chameera
  • 82
  • 8
0

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;
  }
Omkar Naik
  • 11
  • 1
0

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