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.