w3resource

JavaScript: Check a string is a Pangram or not

JavaScript String: Exercise-55 with Solution

A pangram or holoalphabetic sentence is a sentence using every letter of a given alphabet at least once. Pangrams have been used to display typefaces, test equipment, and develop skills in handwriting, calligraphy, and keyboarding.
Write a JavaScript function to check whether a string is a Pangram or not.
Example : "The quick brown fox jumps over the lazy dog"
Test Data:
("The quick brown fox jumps over the lazy dog") -> true
("Waltz, bad nymph, for quick jigs vex.") -> true
("The five boxing wizards jump quickly.") -> true
("The boxing wizards jump quickly.") -> false
(12356) -> "It must be a string."

Sample Solution:

HTML Code:

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>JavaScript function to Check a string is a Pangram or not</title>
</head>
<body>

</body>
</html>

JavaScript Code:

const test = (string) => {
  if (typeof string !== 'string') {
    return 'It must be a string.'
  }
  const result = new Set()
  for (const ch of string.toUpperCase()) 
  {
    if (/[A-Z]/.test(ch)) 
    {
        result.add(ch)
    }
  }
  return result.size === 26
}
console.log(test("The quick brown fox jumps over the lazy dog"))
console.log(test("Waltz, bad nymph, for quick jigs vex."))
console.log(test("The five boxing wizards jump quickly."))
console.log(test("The boxing wizards jump quickly."))
console.log(test(12356))

Sample Output:

true
true
true
false
It must be a string.

Flowchart:

Flowchart: JavaScript: Check a string is a Pangram or not

Live Demo:

See the Pen javascript-string-exercise-55 by w3resource (@w3resource) on CodePen.


Improve this sample solution and post your code through Disqus

Previous: Check a string is in Kebab case.
Next: Check a string is in Pascal case.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.

JavaScript: Tips of the Day

defineProperty method

const person = { name: 'Owen' };

Object.defineProperty(person, 'age', { value: 21 });

console.log(person);
console.log(Object.keys(person));

With the defineProperty method, we can add new properties to an object, or modify existing ones. When we add a property to an object using the defineProperty method, they are by default not enumerable. The Object.keys method returns all enumerable property names from an object, in this case only "name".
Properties added using the defineProperty method are immutable by default. You can override this behavior using the writable, configurable and enumerable properties. This way, the defineProperty method gives you a lot more control over the properties you're adding to an object.

Ref: https://bit.ly/323Y0P6

 





We are closing our Disqus commenting system for some maintenanace issues. You may write to us at reach[at]yahoo[dot]com or visit us at Facebook