Https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/basic-algorithm-scripting/slice-and-splice

I think my code is good, but I get error message: All elements from the first array should be added to the second array in their original order. Why? Thank you

   **Your code so far**

function frankenSplice(arr1, arr2, n) {
let arr3 = [];
 for (let i = 0; i < arr2.length; i++) {
   if (i == n) {
     for (let j = 0; j < arr1.length; j++) {
       arr3.push(arr1[j]);
     }
     arr3.push(arr2[i]);
   } else {
     arr3.push(arr2[i]);
   }
 }
 return arr3;
}

frankenSplice([1, 2, 3], [4, 5, 6], 1);
   **Your browser information:**

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.128 Safari/537.36.

Challenge: Slice and Splice

Link to the challenge:

the test you are not passing has an empty array as arr2, in that case the external loop never happens.
Why don’t you try using slice and splice?

Thank you. I will try that also.

But I don’t understand. The outer loop run too and I get the correct result all scenario. What do you mean? Thank you

let result = frankenSplice([1, 2, 3], [4, 5, 6], 1);
let result2 = frankenSplice([1, 2], ["a", "b"], 1);
let result3 = frankenSplice(  ["claw", "tentacle"],  ["head", "shoulders", "knees", "toes"],  2);
let result4 = frankenSplice(["claw", "tentacle"], [], 2);
let result5 = frankenSplice([], ["claw", "tentacle"], 2);
console.log(result);
console.log(result2);
console.log(result3);
console.log(result4);
console.log(result5);
[ 4, 1, 2, 3, 5, 6 ]
[ 'a', 1, 2, 'b' ]
[ 'head', 'shoulders', 'claw', 'tentacle', 'knees', 'toes' ]
[ 'claw', 'tentacle' ]
[ 'claw', 'tentacle' ]

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (’).

1 Like

in this case it can’t run, i starts at 0, arr2.length is 0 and 0 < 0 is false

I used your code and checked the output, the output is []

Thank you very much!
Really :slight_smile:
I understand this. It’s shouldn’t run.

But i’ts run that case also and I get the correct result. Why? I use visual studio code terminal node v14.16.1

please post the code you are using

function frankenSplice(arr1, arr2, n) {
  let arr3 = [];
  for (let i = 0; i < arr2.length; i++) {
    if (i == n) {
      for (let j = 0; j < arr1.length; j++) {
        arr3.push(arr1[j]);
      }
      arr3.push(arr2[i]);
    } else {
      arr3.push(arr2[i]);
    }
  }
  return arr3;
}
let result = frankenSplice([1, 2, 3], [4, 5, 6], 1);
let result2 = frankenSplice([1, 2], ["a", "b"], 1);
let result3 = frankenSplice(
  ["claw", "tentacle"],
  ["head", "shoulders", "knees", "toes"],
  2
);
let result4 = frankenSplice(["claw", "tentacle"], [], 2);
let result5 = frankenSplice([], ["claw", "tentacle"], 2);
console.log(result);
console.log(result2);
console.log(result3);
console.log(result4);
console.log(result5);

the fourth one is an empty array
image

What’s happening here? I get different result!?

are you sure you have the same code there?

Yes I copy and paste

no sorry.

I put the freecodecamp solution also, but a little under my code. If I cut that two solution I get your result. thank you again :slight_smile:

function frankenSplice(arr1, arr2, n) {
  let arr3 = [];
  for (let i = 0; i < arr2.length; i++) {
    if (i == n) {
      for (let j = 0; j < arr1.length; j++) {
        arr3.push(arr1[j]);
      }
      arr3.push(arr2[i]);
    } else {
      arr3.push(arr2[i]);
    }
  }
  return arr3;
}
let result = frankenSplice([1, 2, 3], [4, 5, 6], 1);
let result2 = frankenSplice([1, 2], ["a", "b"], 1);
let result3 = frankenSplice(
  ["claw", "tentacle"],
  ["head", "shoulders", "knees", "toes"],
  2
);
let result4 = frankenSplice(["claw", "tentacle"], [], 2);
let result5 = frankenSplice([], ["claw", "tentacle"], 2);
console.log(result);
console.log(result2);
console.log(result3);
console.log(result4);
console.log(result5);

/*Jobb megoldás 1*/
function frankenSplice(arr1, arr2, n) {
  // It's alive. It's alive!
  let localArray = arr2.slice();
  for (let i = 0; i < arr1.length; i++) {
    localArray.splice(n, 0, arr1[i]);
    n++;
  }
  return localArray;
}

/*Jobb megoldás 2*/
function frankenSplice(arr1, arr2, n) {
  // It's alive. It's alive!
  let localArr = arr2.slice();
  localArr.splice(n, 0, ...arr1);
  return localArr;
}

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.