多维数组 JavaScript 挑战

huangapple go评论63阅读模式
英文:

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 = [
        [&#39;task 1&#39;, 45],
        [&#39;task 2&#39;, 87],
        [&#39;task 3&#39;, 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 = [
        [&#39;task 1&#39;, 45],
        [&#39;task 2&#39;, 87],
        [&#39;task 3&#39;, 83]
    ];

    tasks.forEach(task =&gt; toDoList.push(task));
    // Do not alter any code below here
    return toDoList;
}

console.log(createToDoList())

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年5月21日 02:54:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/76296882.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定