我已经被这个Twilioquest问题困扰了好几个小时,它包括数组.我该如何解决



所以我最近下载了TwilioQuest,尝试正确学习编程。它进行得很顺利,我认为这是一个学习编程的好游戏,但我遇到了一个问题。

为了进入游戏的下一部分,我需要制作一个程序,其中包含一个函数,该函数接受单个数组命令行参数,并将数组的第一个索引添加到最后一个。它给了我一些启动代码,以备我需要,但即便如此,我还是很困惑。这是它给我的起始代码:

function addFirstToLast(inputArray) {
let firstAndLast = '';
// Only execute this code if the array has items in it
if (inputArray.length > 0) {
// Change the line below! What should it be?
firstAndLast = inputArray[999] + inputArray[999];
}
return firstAndLast;
}
// The lines of code below will test your function when you run it from the
// command line with Node.js
console.log(addFirstToLast(['first', 'second', 'third']));
console.log(addFirstToLast(['golden', 'terrier']));
console.log(addFirstToLast(['cheerio']));
console.log(addFirstToLast([]));

我只是不明白我必须在代码的第一行放什么,它说,让firstAndLast='';

这是我添加到代码中的内容:

function addFirstToLast(inputArray) {
let firstAndLast = inputArray[0] + inputArray[-1];
// Only execute this code if the array has items in it
if (inputArray.length > 0) {
// Change the line below! What should it be?
firstAndLast = inputArray[0] + inputArray[-1];
}
return firstAndLast;
}
// The lines of code below will test your function when you run it from the
// command line with Node.js
console.log(addFirstToLast(['first', 'second', 'third']));
console.log(addFirstToLast(['golden', 'terrier']));
console.log(addFirstToLast(['cheerio']));
console.log(addFirstToLast([]));

然而,它给了我这样的输出:

firstundefined
goldenundefined
cheerioundefined
NaN

有人能帮帮我吗?

欢迎使用Stack Overflow。

我想你在这里寻找的代码行是这样的:

firstAndLast = inputArray[0] + inputArray[inputArray.length-1];

这将创建一个字符串,该字符串的值来自数组的第一个元素,与数组的最后一个元素的值串联(即使在长度为1的情况下这些值相同(。第一个"if"语句是为了避免在没有项的数组中传递的情况(如下面的[](。

用于连接数组的第一个和最后一个元素inputArray[0] + inputArray[inputArray.length-1]


function addFirstToLast(inputArray) {
let firstAndLast = ""

// Only execute this code if the array has items in it
if (inputArray.length > 0) {
// Change the line below! What should it be?
let first = inputArray[0]
let last = inputArray.length === 1 ? "" : inputArray[inputArray.length - 1]
firstAndLast = first + last;
}
return firstAndLast;
}
// The lines of code below will test your function when you run it from the
// command line with Node.js
console.log(addFirstToLast(['first', 'second', 'third']));
console.log(addFirstToLast(['golden', 'terrier']));
console.log(addFirstToLast(['cheerio']));
console.log(addFirstToLast([]));
function addFirstToLast(inputArray) {
let firstAndLast = '';
if (inputArray.length > 0) {
firstAndLast = inputArray[0] + inputArray.pop();
}
return firstAndLast;
}
console.log(addFirstToLast(['first', 'second', 'third']));
console.log(addFirstToLast(['golden', 'terrier']));
console.log(addFirstToLast(['cheerio']));
console.log(addFirstToLast([]));

我不确定他们是否希望这样做,但这就是我完成任务的方式。代码开始将firstAndLast变量定义为空。然后,它检查列表中是否有超过零个项目。如果是,则继续。如果不是,则变量保持为空。然后,它使用inputArray[0]将inputArray中的第一个项添加到变量firstAndLast中然后使用inputArray.pop从列表中提取最后一项并将其放入变量中。

希望这能有所帮助。

function addFirstToLast(inputArray) {
let firstAndLast = '';
// Only execute this code if the array has items in it
if (inputArray.length > 0) {
// Change the line below! What should it be?
firstAndLast = inputArray[0] + inputArray[inputArray.length - 1];    
}
return firstAndLast;
}
// The lines of code below will test your function when you run it from the
// command line with Node.js
console.log(addFirstToLast(['first', 'second', 'third']));
console.log(addFirstToLast(['golden', 'terrier']));
console.log(addFirstToLast(['cheerio']));
console.log(addFirstToLast([]));

上面给出了正确的代码,它只会创建一个字符串,该字符串的值来自数组的第一个元素,与最后一个元素的值串联。

最新更新