2

i have function called sunset that copies the existing array sunsetColors by reference and then changes the first element to "blue". sunset should return the copy of the array.


function sunset(){
  let clone =sunsetColors;
clone.splice(0,0,"blue");
  return clone;
}
sunset()
Divya
  • 31
  • 1
  • well it is not a copy.... `console.log(sunsetColors, clone)` – epascarello May 31 '19 at 16:44
  • what i am doing wrong here ? – Divya May 31 '19 at 16:45
  • 1
    https://stackoverflow.com/questions/7486085/copy-array-by-value – epascarello May 31 '19 at 16:46
  • @Divya just add it with destructuring as such `[ "blue", ...sunsetColors ]` – darklightcode May 31 '19 at 16:46
  • @darklightcode does that replace the first element with "blue"? – GrafiCode May 31 '19 at 16:48
  • it is replacing the first element to "blue. – Divya May 31 '19 at 16:49
  • It will create a new array where "blue" is at position 0 and the rest of the elements will be added by the `...`(spread operator) – darklightcode May 31 '19 at 16:49
  • but i am getting "sunset should create a copy by reference and change the first element to blue" failure message – Divya May 31 '19 at 16:49
  • @darklightcode OP asked "and then changes the first element to 'blue'" – GrafiCode May 31 '19 at 16:50
  • thats what this code do right ? [ "blue", ...sunsetColors ] changing the first element to "blue" – Divya May 31 '19 at 16:53
  • @Divya it actually prepends "blue" to the array, as first element. When you say `change`, you mean `replace the first element` or `place at the beginning`? – GrafiCode May 31 '19 at 16:54
  • My apologies, i may have misread , try this, `clone.splice(0,1,"blue");` , the second parameter of `.splice` will replace the item from position 0 in `clone`. If that doesn't help. Please provide us a sample of `sunsetColors` and the expected outcome. – darklightcode May 31 '19 at 16:56
  • i tried that already , but i am still getting "sunset should create a copy by reference and change the first element to blue" " sunset should not change the rest of the array" "Expected false to be true." – Divya May 31 '19 at 16:59
  • @Divya where exactly are you getting that message from? – GrafiCode May 31 '19 at 17:00
  • jasmine spec runner – Divya May 31 '19 at 17:01
  • If you have an array like `let arr = ['red', 'yellow']`, then you can copy it like `let clone = arr.slice()`. Then you can change the first element of the copy like `clone[0] = 'blue'`. – benvc May 31 '19 at 17:10
  • 2
    i just changed to this and it worked. ``` function sunset() { sunsetColors[0] = "blue"; return (clone = sunsetColors.slice()); } sunset(); ``` thank you all... – Divya May 31 '19 at 17:10

2 Answers2

1

try the following code for cloning

let clone = JSON.parse(JSON.stringify(sunsetColors));

Here is a very good article for array cloning in Javascript.

https://www.freecodecamp.org/news/how-to-clone-an-array-in-javascript-1d3183468f6a/

Jayant Varshney
  • 1,765
  • 1
  • 25
  • 42
0

This does not copy the array.

let clone =sunsetColors;

At the end of that statement, clone and sunsetColors are both pointing to the same array.

To copy the array, try something such as

let clone = [...sunsetColors];

Or you could copy the array this way

let clone = sunsetColors.slice();

Dan Cron
  • 1,105
  • 12
  • 24