It doesn’t work with other inputs because the loop index (i) needed is different.
With this line alone array2Copy.splice(n, 0, arr1); I have the input needed for each input. BUT it doesn’t work. It seems that it is required the brackets to be printed.
suggestions welcome…
Your code so far
function frankenSplice(arr1, arr2, n) {
arr1.slice(0);
let array2Copy = arr2.slice(0);
for (let i = 2; i < arr1.length; i++){
array2Copy.splice(n, 0, arr1);
}
console.log(typeof(array2Copy));
console.log(array2Copy)
return array2Copy;//4, 1, 2, 3, 5
}
frankenSplice([1, 2, 3], [4, 5], 1);
Your browser information:
User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.132 Safari/537.36 OPR/63.0.3368.94.
Great suggestion. I went up one step: So I don’t have the problem index anymore, because I pointed out that the loop should take each element of arra1 into arr2Copy. Now: I don’t understand whether should I turn each array element into a character but stored it in an array, should I?
If so, I tried this with map, trying to map each element to toString. So, what am I doing wrong? If I judge based on what is the screenshots (running on node) It seems that I shouldn’t take anything into string.
function frankenSplice(arr1, arr2, n) {
arr1.slice(0);
let array2Copy = arr2.slice(0);
for (let i = 0; i < arr1.length; i++){
array2Copy.splice(n, 0, arr1[i]);
}
let result = array2Copy.map(i => i.toString());
return array2Copy;
}
console.log(frankenSplice([1, 2, 3], [4, 5], 1));//4,3,2,1,5
frankenSplice([1, 2], ["a", "b"], 1);//a,2,1,b
frankenSplice(["claw", "tentacle"], ["head", "shoulders", "knees", "toes"], 2);