How to resolve the algorithm Pangram checker step by step in the JavaScript programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Pangram checker step by step in the JavaScript programming language
Table of Contents
Problem Statement
A pangram is a sentence that contains all the letters of the English alphabet at least once. For example: The quick brown fox jumps over the lazy dog.
Write a function or method to check a sentence to see if it is a pangram (or not) and show its use.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Pangram checker step by step in the JavaScript programming language
The provided code snippet contains two implementations of a function that checks if a given string is a pangram. A pangram is a sentence that contains every letter of the alphabet at least once.
Implementation 1:
- The
isPangram
function takes a strings
as input. - It initializes a string
letters
containing all the lowercase letters of the alphabet sorted by frequency (ascending). - It converts the input string
s
to lowercase and removes all non-alphabetic characters. - It loops through each letter in the
letters
string and checks if it exists in the processed input strings
. - If any letter is not found in
s
, the function returnsfalse
. - If all letters are found in
s
, the function returnstrue
.
Implementation 2:
- This implementation uses more modern JavaScript features with arrow functions and template literals.
- The
isPangram
function takes a strings
as input. - It uses the
toLowerCase()
method to convert the input string to lowercase. - It uses the
split()
method to create an array of all characters in the alphabet. - It uses the
filter()
method to remove characters from the array that are not present in the lowercase input string. - It returns
0
if the length of the filtered array is0
, indicating that all characters are present. Otherwise, it returns a non-zero value, indicating that not all characters are present.
Usage:
Both implementations are used to check if two example strings ("is this a pangram"
and "The quick brown fox jumps over the lazy dog"
) are pangrams. The expected output is:
false
for the first example (not a pangram)true
for the second example (a pangram)
Source code in the javascript programming language
function isPangram(s) {
var letters = "zqxjkvbpygfwmucldrhsnioate"
// sorted by frequency ascending (http://en.wikipedia.org/wiki/Letter_frequency)
s = s.toLowerCase().replace(/[^a-z]/g,'')
for (var i = 0; i < 26; i++)
if (s.indexOf(letters[i]) < 0) return false
return true
}
console.log(isPangram("is this a pangram")) // false
console.log(isPangram("The quick brown fox jumps over the lazy dog")) // true
(() => {
"use strict";
// ----------------- PANGRAM CHECKER -----------------
// isPangram :: String -> Bool
const isPangram = s =>
0 === "abcdefghijklmnopqrstuvwxyz"
.split("")
.filter(c => -1 === s.toLowerCase().indexOf(c))
.length;
// ---------------------- TEST -----------------------
return [
"is this a pangram",
"The quick brown fox jumps over the lazy dog"
].map(isPangram);
})();
You may also check:How to resolve the algorithm Longest string challenge step by step in the Ring programming language
You may also check:How to resolve the algorithm Polymorphism step by step in the ooRexx programming language
You may also check:How to resolve the algorithm Write to Windows event log step by step in the Ruby programming language
You may also check:How to resolve the algorithm Assertions step by step in the Icon and Unicon programming language
You may also check:How to resolve the algorithm Generic swap step by step in the Forth programming language