Slice and Splice alternate solution?

Slice and Splice alternate solution?
0

#1

Tell us what’s happening:
I could be mistaken, since I’m relatively new at this, but while the solution uses a for loop, wouldn’t this be essentialy the same? I get the “All elements from the first array should be added to the second array in their original order.” error but I don’t see how this would mess up the order. Thank you for any help.
Your code so far


function frankenSplice(arr1, arr2, n) {
  // It's alive. It's alive!
  let copy = arr1.slice();
  let newArr = arr2.slice();
  newArr.splice(n, 0, copy);
  return newArr;
}

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));

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:62.0) Gecko/20100101 Firefox/62.0.

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


#2

So try adding a console.log() just before you return. I can see what’s going wrong, you’re inserting an array at the given position. Not inserting the array elements, but the entire array as that given element.

The line I added (just prior to your return statement) that cleared this up for me looks like:

  console.log(arr1, arr2, newArr);

Are you familiar with the spread operator yet?


#3

Right… Yeah, I see what you mean. I was looking at the console and seeing the expected output but didn’t realize it was inserting an array instead of the values. Now I just feel dumb.
Thank you for the answer!


#4

Don’t feel dumb, we learn best by breaking things. :wink:

Seriously, when you explore other ways of doing things, finding out why something works or doesn’t, that’s when you learn. Not saying the lessons and challenges are pointless, they aren’t – so long as they get you exploring and challenging your (and their) limits.


#5

Yeah, I wasn’t putting myself down or anything, this happen to me all the time. An obvious error will be staring me right in the eye and I won’t see it, but in retrospective it’s really simple and silly. But I’ll get through this and learn. Thanks again :smiley: