英文:
Multidimensional Arrays JavaScript Challenge
问题
I'm doing a challenge on a website, as follows:
>Complete the function createToDoList, so that the returned multidimensional array contains at least three tasks listed as nested arrays.
>Step 1: Create the task sub-arrays.
>The first element of each nested array should be a string describing the task.
The second element should be a number representing how long that task will take in minutes.
>Step 2: Add the arrays to the toDoList.
>Select an appropriate array method that will allow you to add to toDoList.
Here is what I have so far:
function createToDoList() {
const toDoList = [];
// Do not alter any code above here
const tasks = [
['task 1', 45],
['task 2', 87],
['task 3', 83]
];
toDoList.push(tasks);
// Do not alter any code below here
return toDoList;
}
Clearly I'm not understanding something here, I don't know how to push the arrays into the variable (if that's what it is asking me...), I really just don't get it.
Thanks.
英文:
I'm doing a challenge on a website, as follows:
>Complete the function createToDoList, so that the returned multidimensional array contains at least three tasks listed as nested arrays.
>Step 1: Create the task sub-arrays.
>The first element of each nested array should be a string describing the task.
The second element should be a number representing how long that task will take in minutes.
>Step 2: Add the arrays to the toDoList.
>Select an appropriate array method that will allow you to add to toDoList.
Here is what I have so far:
function createToDoList() {
const toDoList = [];
// Do not alter any code above here
const tasks = [
['task 1', 45],
['task 2', 87],
['task 3', 83]
];
toDoList.push(tasks);
// Do not alter any code below here
return toDoList;
}
Clearly I'm not understanding something here, I don't know how to push the arrays into the variable (if that's what it is asking me...), I really just don't get it.
Thanks.
答案1
得分: 1
你已经拥有tasks
中的所需内容,所以你可以直接返回它。由于你不被允许更改代码的某些部分,你可以利用数组是一个 Iterable
并使用展开操作符 ...
将tasks
中的所有元素推入toDoList
中。
你还可以使用 forEach()
:
function createToDoList() {
const toDoList = [];
// 不要更改上面的任何代码
const tasks = [
['task 1', 45],
['task 2', 87],
['task 3', 83]
];
tasks.forEach(task => toDoList.push(task));
// 不要更改下面的任何代码
return toDoList;
}
console.log(createToDoList())
英文:
You already have what you want in tasks
so you could just return that. As you're not allowed to change the some parts of the code you can use the fact that an array is an Iterable
and use the spread operator ...
to push all the elements in tasks
into toDoList
.
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
function createToDoList() {
const toDoList = [];
// Do not alter any code above here
const tasks = [
['task 1', 45],
['task 2', 87],
['task 3', 83]
];
toDoList.push(...tasks);
// Do not alter any code below here
return toDoList;
}
console.log(createToDoList())
<!-- end snippet -->
You could also use forEach()
:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
function createToDoList() {
const toDoList = [];
// Do not alter any code above here
const tasks = [
['task 1', 45],
['task 2', 87],
['task 3', 83]
];
tasks.forEach(task => toDoList.push(task));
// Do not alter any code below here
return toDoList;
}
console.log(createToDoList())
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论