Tell us what’s happening:
I am tiring the run this challenge but the out I am getting, I am confused what is my output vs what should be
I see following in console
// running tests
frankenSplice([1, 2, 3], [4, 5], 1)
should return
[4, 1, 2, 3, 5]
.
frankenSplice([1, 2], ["a", "b"], 1)
should return
["a", 1, 2, "b"]
.
frankenSplice(["claw", "tentacle"], ["head", "shoulders", "knees", "toes"], 2)
should return
["head", "shoulders", "claw", "tentacle", "knees", "toes"]
. All elements from the first array should be added to the second array in their original order.
frankenSplice([1, 2, 3, 4], [], 0)
should return
[1, 2, 3, 4]
. // tests completed // console output [ 4, 5, 1, 2, 3 ] [ 4, 5, 1, 2, 3, 6 ] [ 1, 2, ‘a’, ‘b’ ] [ ‘claw’, ‘tentacle’, ‘head’, ‘shoulders’, ‘knees’, ‘toes’ ] [ 1, 2, ‘a’, ‘b’ ] [ 1, 2, ‘a’, ‘b’ ]
Your code so far
function frankenSplice(arr1, arr2, n) {
//start by printing arr2,
// insert array1 in the location of n of second array
let combined = [];
for(let x = 0; x < arr2.length;x++){
combined.push(arr2[x])
if(x === n){
//if index of x === n
combined.splice(arr2[x],0,arr1)
//combined.push(arr1)
}
}
let result = combined.flat()
console.log(result)
return result;
}
frankenSplice([1, 2, 3], [4, 5, 6], 1);
Your browser information:
User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36
Challenge: Basic Algorithm Scripting - Slice and Splice
Link to the challenge: