Basic Data Structures - Copy Array Items Using slice()

Tell us what’s happening:
understanding the second argument of slice is quite difficult to understand. is the second index counting from the beginning of the array?

Your code so far

function forecast(arr) {
  // Only change code below this line
    arr.slice(2,1)
  return arr;
}

// Only change code above this line
console.log(forecast(['cold', 'rainy', 'warm', 'sunny', 'cool', 'thunderstorms']));

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 Array Items Using slice()

Link to the challenge:

slice() takes only 2 parameters — the first is the index at which to begin extraction, and the second is the index at which to stop extraction (extraction will occur up to, but not including the element at this index).

Yes, indices are measured from the front of the array.

Here you are copying out entries starting at index 2, stopping at index 1 (which is no entries) and then throwing away the result.

1 Like

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