Add Items to an Array with push() and unshift()

Tell us what’s happening:
what did I missing?

Your code so far


function mixedNumbers(arr) {
  // change code below this line
arr.push("I", 2, "three");
arr.unshift(7, "VIII", 9);
  // change code above this line
  return arr;
}

// do not change code below this line
console.log(mixedNumbers(['IV', 5, 'six']));

Your browser information:

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

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-data-structures/add-items-to-an-array-with-push-and-unshift/

1 Like
arr.unshift("I", 2, "three");
arr.push(7, "VIII", 

.push is going to the last array another one not

2 Likes

HI @arduino731,

This is the solution of example:

function mixedNumbers(arr) {
  // change code below this line
  arr.unshift("three");
  arr.unshift(2);
  arr.unshift("I");
  arr.push(7);
  arr.push("VIII");
  arr.push(9);
  // change code above this line
  return arr;
}

// do not change code below this line
console.log(mixedNumbers(['IV', 5, 'six']));

Hope this help you, good luck and keep going up :muscle:t2:

Regards,
Ali Mosaad

3 Likes

The best way needs to be


`arr.unshift("I", 2, "three");
arr.push(7, "VIII", 9);

Blockquote

`

You can use very simple method to pass this test and is very easy.

arr.splice(0, 1 ,“DarkSalmon”);
arr.splice(1, 1, ‘BlanchedAlmond’);

This will save time of yours.

I feel like my code should have passed the test. It uses push() and unshift(), but it uses them with spread operators, which is more readable than multiple lines, at least to me. It passes the output test but doesn’t seem to realize that the required functions were used.

arr.unshift(...['I', 2, 'three']);
arr.push(...[7,'VIII',9]);

I was putting “7” instead of 7.

It’s better to push and unshift all at once instead of one by one.