Slice and Splice Tests Failing

Slice and Splice Tests Failing
0

#1

Tell us what’s happening:
The following code is not passing the test for this algorithm exercise, but the output i’m seeing matches the expected output. Can someone help me understand what I’m doing wrong?

Your code so far


function frankenSplice(arr1, arr2, n) {
  // It's alive. It's alive!
  let x = arr1.slice(0,arr1.length);
  let newArr = [...arr2];
  newArr.splice(n,0,x);
  console.log(newArr);
  return newArr;
}

frankenSplice([1, 2], ["a", "b"], 1); 

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.92 Safari/537.36.

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-algorithm-scripting/slice-and-splice


#2

In the test case you show above, the return value should be ["a", 1, 2, "b"], but your function instead returns [ "a", [ 1, 2 ], "b" ].

You are inserting the entire copy of arr1 array into a copy of array arr2 at the index specified by n instead of just the arr1 elements.


#3

Ah thank you! Is there a way to print the brackets on arrays when you use console.log? My output here just looked like a,1,2,b not [a,[1,2],b].


#4

which browser are you using?


#5

If you use your browser’s console (Ctrl+Shft+J in Chrome) then you would see something like below.

image

I like using node to test my code. When running in node, you would see the following.

image

If you do not wan to install node, but want to use it, I suggest using repl.it.

See below your code using repl.it


#6

This is great! Thank you! I was just using the freecodecamp exercise output area to test my code.


#7

Sometimes that “fake console output area” does not always convey what is really going on. It is always best to use the browser’s console to see what is really going on.