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 | Show 3 more comments8 Answers
Reset to default 5You 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.
- https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/every
- 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"));
for (let beta = 65; beta < 91; beta++) {
– Mark Commented Nov 28, 2018 at 3:36