Manipulate Arrays With unshift() - whats wrong with my code?

Manipulate Arrays With unshift() - whats wrong with my code?
0

#1

Tell us what’s happening:

Your code so far


// Example
var ourArray = ["Stimpson", "J", "cat"];
ourArray.shift(); // ourArray now equals ["J", "cat"]
ourArray.unshift("Happy"); 
// ourArray now equals ["Happy", "J", "cat"]

// Setup
var myArray = [["John", 23], ["dog", 3]];
myArray.shift();

// Only change code below this line.
myArray.unshift("Paul",35);

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36.

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-javascript/manipulate-arrays-with-unshift


#2

It has to be an array. Just wrap it around your values in an array.


#3

I got the solution. I wrote “Paul”,35 like this - (“Paul”,35) , instead of this - [(“Paul”,35)]. But now i have a question . In sample given above they have written it like - ourArray.unshift(“Happy”); . Why no square brackets here ? Just the curv brackets. while i got my solution only after using square brackets. Why so?


#4

var ourArray = ["Stimpson", "J", "cat"];
ourArray is an array of strings.

var myArray = [["John", 23], ["dog", 3]];
myArray is an array of arrays of strings.

The value of myArray after executing myArray.unshift("Paul", 35), your first attempt, would be as follows:

["Paul", 35, ["John", 23], ["dog", 3]]

Do you see how that is different from the expected value as below?

[["Paul", 35], ["John", 23], ["dog", 3]]

#5

i get it. Thanks lots :slight_smile: