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

Tell us what’s happening:

The solution listed below should be a valid solution to the problem, as it provides the expected output. This would likely be the way used in a program, as well, since the data set would likely be another array instead of arbitrary, static variables.

Your code so far

function mixedNumbers(arr) {
  // change code below this line
  arr.unshift(...['I', 2, 'three']);
  arr.push(...[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/74.0.3729.169 Safari/537.36.

Link to the challenge:

You can post solutions that invite discussion (like asking how the solution works, or asking about certain parts of the solution). But please don’t just post your solution for the sake of sharing it.

We have set your post to unlisted. Thanks for your understanding.

My point was that this solution doesn’t work and it will not pass the tests despite providing the correct output. It’s not a valid solution at this point in time.

Ariel, I did say that it should be a solution, which indicates it is not a passing solution.

You could think of opening an issue on GitHub for this, There you could argument your reason to change the tests

Sorry. I misunderstood your post.

What tests are failing?

No problem. I figured that was what happened.

The following code does not pass any test yet provides the expected output and implements a spread operator that was already learned at this point:

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

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

The challenge expects them to be passed as parameters to unshift and push which is not how most programs will operate. Most programs will likely use an array of data to unshift or push on to another data set.

You’re right that your solution should pass, but the test is too detailed regarding implimentation.

This exercise is just intended to introduce the array methods, not to be a realistic function. You’re right that if you were adding multiple items to an array, those values would also be in arrays. Of course, in this case where they are hardcoded values there would be no reason to put them in arrays just to spread those arrays.

I would argue that that is allowing bad habits to be produced by allowing only the hardcoded values to pass rather than an array implementation. But, I’m not going to debate this any further. It should be changed.

I’m confused. A hardcoded array is still a hardcoded value. I’m not arguing that the tests shouldn’t be updated to accept your answer, but I don’t think that hardcoded values are going to be removed from the introductory challenges.

The point is that although those are hardcoded values, the method in which you use them with the spread operator shows a better way of handling data sets. Once you learn about arrays, you realize that the hardcoded array values could be swapped out with a reference to an array from a JSON data set.

It doesn’t matter if nothing changes. I’m going back through these for a bit of a refresher and perhaps to learn some things I didn’t know.

Thanks, Ariel!

I have created a pull request which modified the current tests to allow your solution to pass the challenge. Once the master branch gets deployed to production, this will no longer be an issue.

1 Like

Nice. That PR should also allow for the use of apply() which the current challenge test also doesn’t support.


function somethingSomeThing(arr) {
  Array.prototype.unshift.apply(arr, [1, 'two', 3]);
  Array.prototype.push.apply(arr, [7, 8, 9]);
  return arr;

somethingSomeThing([4, 5, 'six'])

@lasjorg That is a very unlikely edge case since we don’t even cover apply in the curriculum not to mention that is a very ugly solution (no offense).

That isn’t really the point, that solution should pass the test. Point is we are now just matching on /\.push/ and /\.unshift/

@RandellDawson, thank you!