Slice and Splice - correct but not passed

Tell us what’s happening:
this seems to do what the challenge wants it to do, but it does not pass. did i miss something? or using the spread operator is not permitted?

Your code so far

[spoiler]
function frankenSplice(arr1, arr2, n) {
  let temp = [...arr2];
  arr2.splice(n,0,arr1);
  let tempp = [...arr2];
  arr2 = temp;
  console.log(arr1);
  console.log(arr2);
  return tempp;


}

console.log(frankenSplice([1, 2, 3], [4, 5], 1));
console.log(frankenSplice([1, 2], ["a", "b"], 1));
console.log(frankenSplice(["claw", "tentacle"], ["head", "shoulders", "knees", "toes"], 2));
[/spoiler]

Your browser information:

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

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

I suggest looking at some of the recent threads about this challenge. Several people (at least two today I believe) have made the same error that is in your code. (You are not returning the correct arrays in some cases.)

I suggest you to look at your console.log in chrome developer tools. FCC console “stringify” the logged arrays, making them appear flat when they aren’t.

Look at you are actually returning:

42

I’ve changed my code to using solely slice and splice method, but still failed to pass even it’s correct when i check with conosle.log.

About the Stringify issue, does it mean I am not writing the code in the correct or appropriate way? Like having a bad habit that leads to such errors?

function frankenSplice(arr1, arr2, n) {
let temp = arr2.slice(0,arr2.length);
console.log(temp);
temp.splice(n,0,arr1);
return temp;
}

No. What this means is that your code is returning the wrong value.

FCC tests for [4, 1, 2, 3, 5]
You are returning [4, [1, 2, 3], 5] as you can see from the screenshot above

Unfortunately, FCC console will display [4, 1, 2, 3, 5] and [4, [1, 2, 3], 5] the same way: 4,1,2,3,5. But clearly they are two different values.

If you are using chrome you can press F12 and check for the correct console.log value in the console tab. You can also copy your code into codepen and look at the console to check if you’re returning what FCC tests expect.

I understand the problem now! And I am able to pass the test by running a for loop to splice in the item in arr1 one by one. Is there a more appropriate or efficient way to do it? Does it mean that splicing an array variable will always result in creating another dimension of array?

function frankenSplice(arr1, arr2, n) {
let temp = arr2.slice(0,arr2.length);
let tempp = arr1.slice(0,arr1.length);
for(let i = 0; i<tempp.length;i++){
temp.splice(n+i,0,tempp[i]);
}
return temp;
}

Thanks guys!

No, it is just that you’ve added an array to splice.

Example:

var x = [1, 2, 3]
x.splice(1, 0, [4, 5]); // [1, [4, 5], 2, 3]
x.splice(1, 0, ...[4, 5]); // [1, 4, 5, 2, 3]
x.splice(1, 0, 4, 5); // [1, 4, 5, 2, 3]
1 Like