Tell us what’s happening:
The code is working fine but my else condition isn’t working. following is my else if condition
else if(arr2.length == 0){ //here I am checking if the length is 0 that means nothing is in the array then push each item of arr1 into the combo
arr1.map((item)=>{
combo.push(item)
})
}
What am I doing wrong or missing? Your code so far
That’s probably because you did not change the function call at the bottom of your code. Try adding a console.log just above your if condition with the string “test” in it, also maybe item and index as well. Then change that function call from one that is passing to the one that is failing and see if you notice anything.
@mahassan I ran your code solution on the challenge, and it’s only failing with this test:
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].
Your code was traversing the arr2 array first, it’s okay if the array is not empty, but the problem is what if the arr2 is empty? You currently do not have a condition to catch this case.
test 4 0
test 5 1
[ 4, 1, 2, 3, 5 ]
[]
test a 0
test b 1
[ 'a', 1, 2, 'b' ]
test head 0
test shoulders 1
test knees 2
test toes 3
[ 'head', 'shoulders', 'claw', 'tentacle', 'knees', 'toes' ]
[]
test a 0
test b 1
[ 'a', 1, 2, 'b' ]
test a 0
test b 1
[ 'a', 1, 2, 'b' ]
Try it with this console log and these function calls and read up a little on how map works. Do you see why what @arantebillywilson is saying about everything being in map is potentially your problem?
function frankenSplice(arr1, arr2, n) {
//combine the both array
let combo = [];
arr2.map((item, index)=>{
console.log('test if this is running with arrays', arr1, arr2)
if(index == n){
arr1.map((item)=>{
combo.push(item)
})
}else if(arr2.length == 0){
arr1.map((item)=>{
combo.push(item)
})
}
combo.push(item)
})
return combo
}
frankenSplice([1, 2, 3], [4, 5, 6], 1);
frankenSplice([1, 2, 3, 4], [], 0)