从If语句中的数组中删除最后一个对象的正确方法

问题描述

我已经编写了一个脚本来检查一个句子是否是全称。

我正在尝试删除数组中的最后一个元素。为了创建语法上正确的句子,例如“ X,Y和Z”。我尝试使用.pop(),据我所知,它会删除数组的最后一个元素,但也会返回该元素。

尽管使用'let'。 .pop()从原始数组中删除对象,而不仅仅是在if语句中声明的新对象。

希望有人可以帮助您,并为我指出正确的方向。

// 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!')
}

解决方法

您应该使用else if。如果在长度为3时启动第一个if测试,则使用pop()将长度设为2,因此下一个if测试也将启动。此外,您只需要检查是否长度大于一,如果需要,则将其删除,然后使用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!')
}

,

您还可以先尝试使用以下命令克隆阵列:

let newArray = [...missingLetters];

然后您可以使用以下任一选项删除最后一项:

newArray.splice(-1,1);
newArray = newArray.pop();
,

首先... Y尝试向您解释此代码...

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)
}

这是一种简化数组过滤的方法...您需要包含句子中不包含的字母

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

之后,您只有3个案例

  • 当长度为0时
  • 长度为1
  • 长度为2以上

仅在带foreach的if中使用concat最终文本

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)
}

以这种形式更优雅地展示

`The letters ${missingLetter}`