Is array of strings Pangrams

I am trying to solve the problem if the array of strings is pangram, which contains letters a-z. If a string is pangram, return 1, if a string is not pangram, return 0. The following code checking an individual letter in the string, but I encountered the problem to check each string. For example, the “The quick brown fox jumps over the lazy dog” is pangram, give 1, and “This is not a pangram” give 0, after the for loop finished the iteration of two strings, the result should be returned 10

function isPangram(string) {
  let result = []
  const letters = "abcdefghijklmnopqrstuvwxyz".split("")
  for (let i = 0; i < string.length; i++) {
    for (let j = 0; j < letters.length; j++) {
      if (string[i].includes(letters[i])) {
        result.push(1)
      }
      if (!string[i].includes(letters[i])) {
        result.push(0)
      }
    }
  }
  return result.join("")
}

console.log(isPangram(["The quick brown fox jumps over the lazy dog", "This is not a pangram"]))

Answer

If what you are looking to is having the function to work on an array of strings, then you could give an array as argument to the function, like you are doing right now, and then perform an iteration on the given argument, to check if the string is pangram, and if that is the case, append 1 to a string, which is eventually returned.

I also suggest to use regex to check if your string is pangram (credits to this answer).

function isPangram(strArray){ 
    var resultStr = "";
    let reg = /(?=.*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)./i 
    strArray.forEach(function(e) {
        //The value of res is determined by the outcome of the regex check, through a ternary operator
        let res = (reg.test(e)) ? "1" : "0";
        resultStr += res;
    })

    return parseInt(resultStr);
}

console.log(isPangram(["The quick brown fox jumps over the lazy dog","This is not a pangram"]))

You may unwrap the resultStr from the parseInt() function if you’d rather have your result as string.

Or if you would like to keep your current loop structure, you could and all the results together, and add the finally obtained one to a string:

function isPangram(strArray) {

var resStr = "";
const letters = "abcdefghijklmnopqrstuvwxyz".split("");

for (let i in strArray) {
    var tempRes = true;
    for (j in letters) {
        tempRes = tempRes & (strArray[i].toLowerCase().includes(letters[i]))
    }
    resStr += tempRes;
}

return resStr
}

console.log(isPangram(["THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG", "This is not a pangram"]))

As also pointed out by @Johnny Mop, if you instead used the condition (strArray[i].includes(letters[i])) to determine the first time a 0 is obtained, you could break the loop to save some time (not really much though, in this case). And as well he’s right to point out that you’ll need toLowerCase(), otherwise THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG won’t result as pangram.

Leave a Reply

Your email address will not be published. Required fields are marked *