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.
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.
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.