Slice and Splice. I need help

Tell us what’s happening:
I’ve tried to run the tests but my browser is not responding. It must be my code. This is the only I’ve had this browser problem.

Your code so far


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

frankenSplice(["claw", "tentacle"], ["head", "shoulders", "knees", "toes"], 2);

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36.

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

Right, your browser crashes because whenever you execute the code, you encounter an infinite loop. The problem is inside your forloop when you splice the array.

You are adding an item to certain index so the size of your array increases every time but the condition of your forloop is run until you reach the end of the array length. So it will never end. Keep working on it :slight_smile: and come back if you still can’t figure it out!

first of all in this line in your code:
var newArr = [];
newArr.push(arr2); // here you are pushing the whole array to newArr so newArr = [[“head”, “shoulders”, “knees”, “toes”]] , so newArr.length =1 not 4 as you may thought.

Tell us what’s happening:
So I almost got the answer but one of the strings is missing ‘tentacle’.

Your code so far


function frankenSplice(arr1, arr2, n) {
   var brandNewArr = [];
      for(let i = 0; i < arr1.length; i++) { 
          var newArr = [];
          arr2.splice(n+i, 0, arr1[i]);
          newArr.push(arr2);
          for(let j = 0; j < newArr.length; j++) {
              return newArr[j];
          }
          brandNewArr.push(newArr[j]);  
      }
      
    return brandNewArr;
}

frankenSplice(["claw", "tentacle"], ["head", "shoulders", "knees", "toes"], 2);

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36.

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

Instead of creating several topics for the same challenge, it’s best to continue an ongoing conversation. I have merged your two most recent topics.

Usually I don’t try to change the logic of the questioner’s code. But you’re using nested for loops, which you should avoid unless absolutely necessary.

Plus this is a good time to think about transforming data so that you can work on it easier.

So I suggest you first think about this

  • they want you split an array in two sections
  • then insert another array between those sections

How can you structure your data so that you don’t have to loop?

Hint: You can split into multiple variables