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));
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));
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
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;
}
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]"
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:
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).
Call i inside the foor loop to add the elements to our new array.
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));
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!
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
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
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
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