Explanation of hint 2 and 3-Splice and Slice

Tell us what’s happening:
Could anyone explain what does this means?

Hint 2
Loop through all of the items in the first array. For each item in the first array splice it into the copied array in the index given as argument.
Hint 3
Increment the index after performing the splice.

Advice, please

Your code so far

function frankenSplice(arr1, arr2, n) {
  let newArray = arr2.slice()   //1. Copy all the items of arr1 into the second arrary arr2, this also make sure that they are not mutated 
  for(let i= 0; i <= arr1.length; i++) { // 2. Loop through all of the items in the first array.
   arr1[1] = newArray.splice()
 //How to increment the index after performing the splice??

  }
  return newArray;
}
console.log(frankenSplice([1, 2, 3], [4, 5,], 1));

function frankenSplice(arr1, arr2, n) {
return arr2;
}

frankenSplice([1, 2, 3], [4, 5, 6], 1);

Your browser information:

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

Challenge: Slice and Splice

Link to the challenge:

splice needs some arguments
you also don’t need the returned value, it will change on its own the value of the newArray element

to increment the index you just need to do n++ but that is not the issue, you need to use first splice correctly

1 Like

The given argument to use with splice() is n
So I should use n as the only argument right?

Could you provide some context how these links are relevant to this question? By themselves they give a somewhat spammy impression … Please provide more context, or remove them …

function frankenSplice(arr1, arr2, n) {
  let newArray = arr2.slice()//1. Copy all the items of arr1 into the second arrary arr2, this also make sure that they are not mutated 
  for(let i = 0; i >= arr1.length; i++) {// 2. Loop through all of the items in the first array.
  newArray.arr1[i] = arr1.splice(n++)//Increment the index after performing the splice??

  }
  return newArray;
}
console.log(frankenSplice([1, 2, 3], [4, 5,], 1));

Which arguments can I use in splice()?
I am lost at HINT 2

please anser to @michaelsndr question


you have some syntax issues there, now it has gotten worse

splice works like this:

let a = [1, 2, 3];
a.splice(1, 0, 'a')
// the arguments of splice are first, the index at which to splice, then the number of elements to remove, and then a list of the elements to add
console.log(a); // now a is equal to [1, 'a', 2, 3]

you should not assign the value returned from splice to anything

also, newArray.arr1[i] will give you an error, you can just delete it as you don’t need it

1 Like

Thank you!
I get it now. I have changed my code to a more effective way:

function frankenSplice(arr1, arr2, n) {
  let newArray = arr2.slice()//1. Copy all the items of arr1 into the second arrary arr2, this also make sure that they are not mutated 
  for(let i = 0; i <= arr1.length; i++) {// 2. Loop through all of the items in the first array.
  newArray.splice(n, 0, arr1)//for each item in the first array splice into the copied array in the index given as an argument
  //Increment the index after performing the splice
   
  }
  return newArray;
}

but does it work? I would say no.
Why?

few things:

  • your loop goes from i = 0 to i = arr1.length, but when you iterate over an array, you should never arrive to arr1.length as arr1[arr1.length] is undefined. It should arrive to arr1.length - 1
  • you never use the i variable inside the loop, so there is no reason to have a loop
  • you are inserting the whole array, multiple times, instead of the elements of the array, resulting in [4,[1,2,3],[1,2,3],[1,2,3],[1,2,3],5,6]"
1 Like

I understand what you said.
So arr1.length is wrong because I am not iterating through all the elements, with -1 I am iterating till the last element. Is that correct?
Also, it makes sense your point on not using i. If I am using a for loop with i, then I must use it to indicate that I want to iterate through arr1.
Learnings:

  1. When working with arrays always count the index at which you want to start the splice or adding elements, in this challenge, i does start at index n= 1 so i = 1 (position 5).
  2. Call i inside the foor loop to add the elements to our new array.
  3. First have clear at what index you want to start adding elements, and then define the foor loop according to this.
    What I did was to adjust the code to:
function frankenSplice(arr1, arr2, n) {
  let newArray = arr2.slice()//1. Copy all the items of arr1 into the second arrary arr2, this also make sure that they are not mutated 
  for(let i = 1; i <= arr1.length; i++) {//2. Loop through all of the items in the first array.
  newArray.splice(n++,0, i)//for each item in the first array splice into the copied array in the index given as an argument //3.Increment the index after performing the splice
  
  }
  return newArray;
}
console.log(frankenSplice([1, 2, 3], [4, 5,], 1));

Thank you for your patience and guidance iaalleen :slight_smile:

let num = 0;
let array = ["a","b","c","d","e","f","g","h","i"]

console.log(array[-1]) // this is JavaScript not Python :] index -1 === undefined

for (let i = 0; i < 9; i++) {
  console.log(num,num+i,array[1],array[i])
}
run this!

Dorfiee this is a bit confusing for me.
Why dont you use the function?

My current sol does not work yet for condition 3 heads and tentacles.

I feel you don’t know what for loop does, this is just a plain demonstration

no, you are iterating over more than the elements, and arr1[arr1.length] is undefined, and you are not interested in an undefined value, right?
so you should use condition i < arr1.length so you don’t use that

also, you are interested in the element inside the array, so arr1[i], not just the value of i

1 Like

How using i <= arr1.length iterates through more than the elements of the arr?

It worked now but I dont get that, please explain :wink:

Thank you!

array are 0 indexed
I will write an array in a column to show the index numbers

let arr = [ 9,    // index 0
3,   // index 1
...
"second to last element",  // index arr.length-2 
"last element"   // index arr.length-1
]   // there is not an index of arr.length

I get that arrays are 0 indexed but I am not writing i >= arr1.length
I am writing i <= arr1.length

I used i < arr.length with index i = 0 and it worked!

How putting <= iterates through more elements? sorry i dont get that

the loop keep iterating as far as the condition is true
arr.length <= arr.length is true so the loop will iterate for the last time for i = arr.length with the issue that in that case arr[i] is undefined

1 Like

So if I understood the loop should stop at some point and if I use <= it will not stop causing the issue of undefined.
with i < arr.length iterates only until the last element of the arr as it does not find more elements to iterate through (false and exit the loop).
Is this correct?

i is just a number, and the loop just execute the same block of code with different values of i
if you use i <= arr.length the value of i will go out of range of the indexes in the array. if you use i < arr.length the values will just be those of the indexes, with nothing extra

1 Like