I’m trying to understand the directions for this algorithm and one of the directions is confusing me:
“Begin inserting elements at index n of the second array.”
The splice() and slice() I am looking up and working through these, but can someone explain this a bit differently. I’m unsure if I should use splice() with the index n with arr1 as one of the parameters. Or, if n needs to be used as a parameter when I splice from arr1 or if all of the above is wrong and it means something else.
The examples in the MDN docs should help, just in case you haven’t already read these docs.
The challenge wants you to take the first array and put the elements from it somewhere inside the second array.
Splice will allow you to do this and slice will help you to make a copy of the array(s) since it doesn’t want you to mutate the arguments.
There are a few different ways you could do this. If you aren’t already use an environment you can experiment with like https://repl.it/ or chrome snippets or a local html + js files combo etc.
I’m a bit perplexed as to why what I’m doing wrong.
I created new variables so that could slice and splice from arr1 & arr2 without altering the original arrays. The issue is probably in how I’m trying to insert a variable into the method parameters of another variable…yet, I don’t understand why.
Help please.
function frankenSplice(arr1, arr2, n) {
// It's alive. It's alive!
var newArr = arr1.slice(0,3);
var newArr2 = arr2.splice(0,3,newArr);
console.log(newArr2)
return arr2;
}
frankenSplice([1, 2, 3], [4, 5, 6], 1);
So, it’s not working and I need to do some more thinking but I’m not sure why I’m failing the test concerning keeping the original array of arr2.
I have arranged so that array has been kept within keepArr, so that it is preserved. Where am I going wrong with this?
Otherwise, I attempted to save my splice() within the keepArr2 container, but it registers nothing when I console.log it. Yet, when I console.log arr2 it returns something that’s almost a right answer [4,5,1,2,6]
I guess I’m thinking that what I’ve written should really return something entirely different than what is actually returning. I think this is something to do with how I’m using my variable containers but I’m not sure exactly where that fault lies.
function frankenSplice(arr1, arr2, n) {
// Make a copy of the second array
const keepArr = arr2.slice(0,3);
//Make loop to run through first array
for(let i = 0;i < arr1.length; i++){
//splice each items into the array as an argument
if(i>0){
let keepArr2 = arr2.splice(-1,0,i);
}
}
console.log(keepArr2);
return keepArr2;
}
frankenSplice([1, 2, 3], [4, 5, 6], 1);
This is my code but still wondering why it didnt work???
unction frankenSplice(arr1, arr2, n) {
// It’s alive. It’s alive!
var sliced = arr2.slice();
var result = sliced.splice(n, 0, …arr1);
return result;
}
frankenSplice([1, 2, 3], [4, 5, 6], 1);
I created a new variable to make a copy of arr2 using slice. So that array is not changed, but what is the missing thing over here???
I know, but I can slice both arr1 and arr2 into new variables, so arrays stay in original way, and then apply splice on those variables if you understand me, but still not working…
try using console.log() to check the value of your variables
I have added them to your code now, copy and paste this and see what the values are, result is not what you think.
function frankenSplice(arr1, arr2, n) {
// It’s alive. It’s alive!
var sliced = arr2.slice();
console.log("before splicing 'sliced' is [" + sliced + "]");
var result = sliced.splice(n, 0, ...arr1);
console.log("'result' is [" + result + "]");
console.log("after splicing 'sliced' is [" + sliced + "]");
return result;
}
frankenSplice([1, 2, 3], [4, 5, 6], 1);