0

Summary of problem:

Essentially I want to perform the following function:

function isPangram(string){
  if (all characters a - z are present in the string) {
    return true;
  } else {
    return false;
  }
}

Here's what I've tried thus far:

function isPangram(string){
if (string.contains(/[a-z]/)) {
    return true;
  } else {
    return false;
  }
}


function isPangram(string){
  if (string.matchAll(/[a-z]/) {
    return true;
  } else {
    return false;
  }
}


function isPangram(string){
  for (let i = 0; i < string.length; i++) {
    if (string[i] = /[a-z]/) {
       return true;
  } else {
    return false;
  }
}

I've laid out the problem and I have the solution but I'm unaware of the proper syntax that allows me to get the solution I'm looking for. So! I'm asking for syntax recommendations that can help lead me in the right direction. I can struggle from there~

Kyle
  • 1
  • 1
  • Your code tests if all the characters are letters, it doesn't test if it has *all* the letters. – Barmar Sep 23 '22 at 19:48

2 Answers2

0

Try using this expression:

new Set("<your_string>".toLowerCase().replace(/[^a-z]/g, "") ).size === 26
Sean
  • 1,368
  • 2
  • 9
  • 21
0

A way to accomplish that could be iterating all characters in test string, and match each character against an "alphabet" array. If character is found, then it gets removed from the alphabet.

When all characters are scanned, if alphabet doesn't contain any elements, it means test string contained all characters in the alphabet.

const alphabet = []

//const testString = '123badcfehgjilknmporqtsvuxwzy000'
const testString = '123badcfehgtsvuxwzy000'

// POPULATE alphabet WITH ASCII CHARACTERS CODES FROM a TO z
for (let i = 97; i < 123; i++) {
  alphabet.push(i)
}

// SCAN ALL CHARACTERS IN testString
for (let i = 0; i < testString.length; i++) {
  let pos = alphabet.indexOf(testString[i].charCodeAt(0))
  if (pos != -1) {
    // IF CURRENT CHARACTER IS PRESENT IN OUR alphabet ARRAY, REMOVE IT
    alphabet.splice(pos, 1)
  }
}

if (alphabet.length == 0) {
  console.log('All alphabet letters found in test string')
} else {
  console.log('These alphabet letters were not found: ', alphabet.map(c => String.fromCharCode(c)) )
}
GrafiCode
  • 3,307
  • 3
  • 26
  • 31