Alright, here’s what I feel unconfirmed about.
when I use splice at different line, the console shows different outcome.
function htmlColorNames(arr) {
// Only change code below this line
arr.splice(1,3)
// Only change code above this line
return arr;
}
console.log(forecast(['cold', 'rainy', 'warm', 'sunny', 'cool', 'thunderstorms']));
Here it would return: [ ‘DarkGoldenRod’, ‘FireBrick’ ] as intended.
On another hand if I make the function return as follows:
function htmlColorNames(arr) {
// Only change code below this line
// Only change code above this line
return arr.splice(1,3);
}
The console would show: [ ‘WhiteSmoke’, ‘LavenderBlush’, ‘PaleTurquoise’ ].
So for a splice, what I understand is
arr.splice(1,3) → return arr means that I have removed index 1 element and removed other 3 counting from the start in that original arr variable.
On the other hand,
return arr.splice(1,3) means that I show what splice has been removed in that splice(1,3) from the original array.
But since slice() doesn’t change anything from the original array,
function forecast(arr) {
// Only change code below this line
arr.slice(1,3)
return arr;
}
The slice here doesn’t affect or alter the original content in arr variable. When I tell the function to return arr, that’s why nothing change.
To see what slice has extracted from the original arr, it has to be told what slice extracted in return is this correct?
function forecast(arr) {
// Only change code below this line
return arr.slice(1,3);
}