0

So I am trying to get this problem solved it's a simple question but it keeps saying I'm not doing it correctly. The problem wants me to take the indexed value of the array.(In this case "batman") and copy it's value into an array. So my first thought was to use .slice() to do the job. But when I console.log() after slicing it into a variable(var thirdHero) I see that what was put into the variable was ["batman"] instead of just "batman". Any help as to how to do this problem would be so 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);

3
  • Some people don't like opening links within questions. Any chance you can display the relevant source code in your question? Thank you Commented Jan 16, 2017 at 0:36
  • 1
    Please share the code in your question instead of hosting it on third party sites. Commented Jan 16, 2017 at 0:37
  • thirdHero is an array so you should be using console.log(thirdHero[0]); to display the value rather then having ["batman"] as the output. Not sure why you're slicing the array so many times. You can select from the array using the index... Commented Jan 16, 2017 at 0:41

1 Answer 1

2

you don't need to slice an array to get a reference to just a single value from it.

'slice' function returns you an array itself

just do

var hero = heroes[2];
Sign up to request clarification or add additional context in comments.

3 Comments

Awesome thank you.. I knew it was something simple! Still learning the basics , sometimes the simple things slip past you hah!
@GabrielLang don't wary about it too much, you will get there :) I used to over-complicate things at first too :)
@GabrielLang If you're just learning some basics then maybe this will help you repl.it/FKEm

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.