Tell us what’s happening:
I’m fairly certain the code I have is valid.
Made a copy of arr2 so we don’t directly modify it, then using splice to insert at n, then return arrCopy
Your code so far
function frankenSplice(arr1 = [], arr2 = [], n = 0) {
// It's alive. It's alive!
const arrCopy = [...arr2]
arrCopy.splice(n, 0, arr1);
return arrCopy;
}
const results = [
frankenSplice([1, 2, 3], [4, 5], 1),
frankenSplice(["claw", "tentacle"], ["head", "shoulders", "knees", "toes"], 2),
frankenSplice([1, 2], ["a", "b"], 1)
];
results.forEach(result => console.log(result));
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36
.
Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-algorithm-scripting/slice-and-splice/
For test1,
Notice you are also creating another array and returning
=> [ 4, [ 1, 2, 3 ], 5 ]
instead of
[4, 1, 2, 3, 5].
1 Like
Expanding on what @shimphillip showed you, after the first and second arguments are supplied to the splice method, you can pass in extra argument values which you want to insert into the array at the index specified by the first argument. An array counts as a single argument. Think how you could “spread” arr1 so it’s elements are the extra arguments to the splice method.
1 Like
This is really helpful! I should honestly switch to an editor when debugging this because this console.log doesn’t show me what you put.
Are you using your browser’s console? If so, then you would definitely see the result @shimphillip is referring to.
Nah I was console.log’ing with FreeCodeCamps, it didn’t show the results inside brackets like how @shimphillip had suggested
The only true way of seeing the results of console.log is to use your browser’s console. In Chrome, you use Ctrl+Shft+J to access the console.
Hi, i have some problems with this test. I think i right, but i don’t pass it. What is my error?. Thanks
Your code so far
function frankenSplice(arr1, arr2, n) {
var arr=[];
arr.push(arr2.slice(0,n));
arr.push(arr1);
arr.push(arr2.slice(n));
console.log(arr);
return arr;
}
frankenSplice([1, 2, 3], [4, 5, 6], 1);
ilenia
January 19, 2019, 11:11am
9
Look at the browser console. arr
has some subarrays
1 Like