0

我写了一个脚本来检查一个句子是否是一个 pangram。

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

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

希望有人可以提供帮助并为我指明正确的方向。

// Pangram 检查器

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

4

3 回答 3

0

你应该使用else if. 如果第一个if测试在长度为 3 时开始,那么 usingpop()使长度为 2,因此下一个if测试也开始。此外,您只需要检查长度是否大于 1,如果需要,删除一个并使用显示其余部分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!')
}

于 2020-10-03T19:42:56.317 回答
0

首先...您尝试向您解释这段代码...

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 来连接最终文本

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

而且这种形式的 concat 更优雅

`The letters ${missingLetter}`
于 2020-10-03T19:52:18.370 回答
0

您也可以尝试先使用以下方法克隆阵列:

let newArray = [...missingLetters];

然后,您可以使用以下任一方法删除最后一项:

newArray.splice(-1,1);
newArray = newArray.pop();
于 2020-10-03T19:48:37.993 回答