Итак, я недавно загрузил 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([]));
Я просто не понимаю, что мне нужно вставить в первые строки кода, где говорится, let 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
Кто-нибудь может мне помочь?