I'm really struggling to solve this problem that should be simple, but I keep getting it wrong. The task is to take the indexed value of an array (in this case "batman") and copy its value into another array. Initially, I tried using .slice()
but when I checked the result by logging it after assigning it to a variable (var thirdHero
), I realized that instead of just "batman"
, it was returning ["batman"]
. Any guidance on how to approach this issue would be greatly appreciated!
// #7 Create an array of strings that are the 7 primary colors in the rainbow - red, orange, yellow, green, blue, indigo, violet (lower-case). Call your array rainbowColors
var rainbowColors = ['red', 'orange', 'yellow', 'green', 'blue', 'indigo', 'violet'];
// #8 Using this array do the following
var heroes = ['superman', 'batman', 'flash'];
// add 'wonderwoman' to the end
heroes.push('wonderwoman');
console.log(heroes);
// remove 'superman' and store him in a variable called firstHero
var firstHero = heroes.shift();
// add 'spongebob' to the start of the array
heroes.unshift('spongebob');
// remove 'flash' from the array and store him in a variable called secondHero
var secondHero = heroes.splice(2, 1);
console.log(heroes);
// leave batman in the array but put a copy of him on a variable called thirdHero
var thirdHero = heroes.slice(1, 2);
console.log(thirdHero);