I have no idea why constructing a new array from scratch using the push() function and for() loops doesn’t work.
function frankenSplice(arr1, arr2, n) {
let arrFin = [];
for (var i = 0; i < arr2.length; i++) {
if (i === n) {
for (var j = 0; j < arr1.length; j++) {
arrFin.push(arr1[j]);
}
}
arrFin.push(arr2[i]);
}
return arrFin;
}
It satisfies all of the use-cases and doesn’t mutate either of the input arrays; for some reason, however, the condition that “ALL elements from the first array should be added to the second array in their original order” doesn’t want to validate, despite the fact that this is exactly what my code seems to accomplish. Is there some unlisted edge-case my code doesn’t work for or is the challenge just mad I didn’t use Slice and Splice like it wanted me to? If it’s the latter, I think the challenge could have done a better job of conveying that requirement in its challenge parameters.
UPDATE: I’m fairly certain it’s because it’s checking for if I added the contents of the first array to the second after already starting with the latter instead of building the entire finished product up piecemeal, which is… frustrating, seeing as my code still produces correct results.
User Agent is:Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.159 Safari/537.36
It has not been deployed yet, but in next update the test will show the function call used in the test
All elements from the first array should be added to the second array in their original order. frankenSplice([1, 2, 3, 4], [], 0) should return [1, 2, 3, 4].
Do you understand now why it doesn’t work?
You can do it with a loop, but nested loops will not work - I imagine you would need like three separated loops and careful use of the n parameter
Ah, I’d see why my solution would fail if one of the arrays were empty. I already used an answer the test “wanted” to pass, but now I know some additional if() clauses might have patched this apparent hole (such as “if one of the 'arr’s has a length of 0, just return the other one ”). Still, it’s frustrating that this edge-case wasn’t spelled out to begin with.