0

I have written a script to check whether a sentence is a pangram or not.

I am trying to remove the last element in an array. In order to create a gramatically correct sentence e.g. 'X, Y and Z.' I have tried using .pop() which as I understand removes the last element of the array but also returns the element.

Despite using 'let'. The .pop() removes the object from the original array, not just the new one declared within the if statement.

Hope someone can help out and point me in the right direction with this one.

// Pangram Checker

var sentence = (('Two rien Jocks hlp fx my big quiz').toUpperCase()).split("")
var alpha_array = ('ABCDEFGHIJKLMNOPQRSTUVWXYZ').split("")

missingLetters = [];

for (var i = 0; i < alpha_array.length; i++) {
    if (sentence.includes(alpha_array[i])) {
    } else {
        missingLetters.push(alpha_array[i])
    }
}
if (missingLetters.length >=3) {
    let arr = missingLetters.pop()
    console.log('The letters ' + arr + ' and ' + missingLetters[(missingLetters.length - 1)] + ' are missing from the sentence.')
}
if (missingLetters.length ==2) {
    console.log('The letters ' + missingLetters[0] + ' and ' + missingLetters[1] + ' are missing from the sentence.')
}
if ( missingLetters.length ==1) {
    console.log('The letter ' + missingLetters[0] + ' is missing from the sentence.')
}
if (missingLetters.length == 0) {
    console.log('This sentence is a pangram, it contains all the letters of the Alphabet!')
}
DCR
  • 14,737
  • 12
  • 52
  • 115
j0hn209
  • 1
  • 1

3 Answers3

0

You should use else if. If the first if test kicks in when the length is 3, then using pop() makes the length 2, so the next if test also kicks in. Additionally, you only need to check if the length is greater than one and, if you want to, remove one and display the rest using missingLetters.join(""):

// Pangram Checker
var sentence = (('Two rien Jocks hlp fx my big quiz').toUpperCase()).split("")
var alpha_array = ('ABCDEFGHIJKLMNOPQRSTUVWXYZ').split("")

missingLetters = [];

for (var i = 0; i < alpha_array.length; i++) {
    if (sentence.includes(alpha_array[i])) {
    } else {
        missingLetters.push(alpha_array[i])
    }
}
if (missingLetters.length > 1) {
    let arr = missingLetters.shift();
    console.log('The letters ' + arr + ' and ' + missingLetters.join("") + ' are missing from the sentence.')
} else if (missingLetters.length == 1) {
    console.log('The letter ' + missingLetters[0] + ' is missing from the sentence.')
} else {
    console.log('This sentence is a pangram, it contains all the letters of the Alphabet!')
}
ATD
  • 1,344
  • 1
  • 4
  • 8
0

You can also try cloning the array first using:

let newArray = [...missingLetters];

Then you can remove the last item using either of these:

newArray.splice(-1,1);
newArray = newArray.pop();
Dalton Whyte
  • 657
  • 1
  • 8
  • 15
0

First... Y try to explain you this code...

var sentence = (('Two rien Jocks hlp fx my big quiz').toUpperCase()).split("")
var alpha_array = ('ABCDEFGHIJKLMNOPQRSTUVWXYZ').split("")

missingLetters = alpha_array.filter(x => !sentence.includes(x));

var finalText = ""

if(missingLetters.length == 0){
    console.log('This sentence is a pangram, it contains all the letters of the Alphabet!')
}else if(missingLetters.length == 1){
    console.log('The letter ' + missingLetters[0] + ' is missing from the sentence.')
}else{
  let count = 0;
  missingLetters.forEach(missingLetter => {
    finalText += count++ == 0 ? `The letters ${missingLetter}` : ` and ${missingLetter}`;
  })
  
  finalText += ` are missing`
  
  console.log(finalText)
}

This is a simplified way to filter an array... you need to have the letters that not are included in sentence

missingLetters = alpha_array.filter(x => !sentence.includes(x));

After that you only have 3 cases

  • When length is 0
  • When length is 1
  • When length is 2 or more

only use an if with a foreach to concat the final text

var finalText = ""

if(missingLetters.length == 0){
    console.log('This sentence is a pangram, it contains all the letters of the Alphabet!')
}else if(missingLetters.length == 1){
    console.log('The letter ' + missingLetters[0] + ' is missing from the sentence.')
}else{
  let count = 0;
  missingLetters.forEach(missingLetter => {
    finalText += count++ == 0 ? `The letters ${missingLetter}` : ` and ${missingLetter}`;
  })

  finalText += ` are missing`

  console.log(finalText)
}

and it's more elegant concat in this form

`The letters ${missingLetter}`
Derek Menénedez
  • 2,003
  • 11
  • 20