Basic Data Structures - Copy an Array with the Spread Operator

Tell us what’s happening:
Describe your issue in detail here.
I do not understand what this codes is all about.
num is an argument for 2, while arr is an argument for [true, false, true].
but i can not see any spread operator or slice method
Your code so far

function copyMachine(arr, num) {
  let newArr = [];
  while (num >= 1) {
    // Only change code below this line

    // Only change code above this line
    num--;
  }
  return newArr;
}

console.log(copyMachine([true, false, true], 2));

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/115.0.0.0 Safari/537.36 Edg/115.0.1901.188

Challenge: Basic Data Structures - Copy an Array with the Spread Operator

Link to the challenge:

Tell us what’s happening:
Describe your issue in detail here.
Or let me humbly say I do not totally understand the function of the while loop here.
while 2 >= 1
something happens …
kind of confused here

Your code so far

function copyMachine(arr, num) {
  let newArr = [];
  while (num >= 1) {
    // Only change code below this line

    // Only change code above this line
    num--;
  }
  return newArr;
}

console.log(copyMachine([true, false, true], 2));

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/115.0.0.0 Safari/537.36 Edg/115.0.1901.188

Challenge: Basic Data Structures - Copy an Array with the Spread Operator

Link to the challenge:

For our function, we want to copy an array a set number of times.
That is what the num parameter is supposed represent.

the while loop is there because while num is greater than or equal to 1, then we are going to copy the array and add it to the newArr.

For example in this function call here

we are going to copy the array [true, false, true] 2 times
we need to add that copied array to the newArr 2 times.

Your result should be

[ [ true, false, true ], [ true, false, true ] ]

hope that helps

1 Like

Thank you jwilkins. But pls can you help with another example because I am still quite confused. It is not really about the answer but how the function works to get to the answer. In case of next time I am confronted with a similar code

can explain what parts of the code are confusing you?

Are you confused about the while loop?
Or how to use the spread operator?

kindly explain how the while loop works in the code first. Also I am not seeing a spread operator in any line of this function

We need the while loop here

because we need to keep track of how many times the copied array is going to be pushed into newArr

As long as num is greater than or equal to 1, then we will continue to copy the array and push it to the newArr.

That is because it is not there.

You have to add the spread operator
that is the challenge

Inside the while loop, you will need to use the spread operator to copy the array

1 Like

it sounds like the bulk of your confusion is that you don’t have the full answer yet

once you get the test to pass and can see the full answer, then you will start to understand the full code better

Give an example of this explanation pls

Here is another example of the spread operator

Let’s say we had a list of numbers like this

const listOfNumbers = [1,2,3,4,5]

and I wanted to copy that list of numbers to a new variable called newListOfNumbers

let newListOfNumbers = [...listOfNumbers]

now newListOfNumbers would result in the array [1,2,3,4,5]

In this challenge, you need to copy the arr using the spread operator.
You also need to use the array method for adding new elements to the end of the array.

When you do those two things, then the test will pass

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.