Slice and Splice - Basic Algorithm Scripting - Free Code Camp

preview_player
Показать описание
In this basic algorithm scripting tutorial we do an exercise called slice and splice. This is another tutorial that makes up a series where I cover the FreeCodeCamp curriculum. Enjoy!
Рекомендации по теме
Комментарии
Автор

Cheers again! :)
My sol was:
function frankenSplice(arr1, arr2, n) {
let splicedArr = [...arr2];
splicedArr.splice(n, 0, ...arr1);
return splicedArr;
}

frankenSplice([1, 2, 3], [4, 5, 6], 1);

calin
Автор

This is by far the simplest one to understand. Thank you so much :D

irfanshuhaimi
Автор

wow.. this was simple and complex at the same time

alvirfrancisco
Автор

that explanation at the end was fantastic. Thank you!

palumasound
Автор

Thank you Mr. Ian a little bit more simple this lesson was for me
function frankenSplice(arr1, arr2, n) {
let result = [];
result.push(...arr2.slice(0, n));
result.push(...arr1);
result.push(...arr2.slice(n, arr2.length));
return result;
}
console.log(frankenSplice([1, 2, 3, ], [4, 5, 6, ], 1));

zken
Автор

Thank you for explaining the code with

mitubes
Автор

i couldnt of figured this out on my own. It just doesnt come to me idk why. grrrr infact im just follwing your videos on most of them, and understanding the logic when you explain it. Thank you for doing this

kieran
Автор

thanks for the video! your videos have definitely made FCC alot easier to absorb! I have a question though, are we supposed to be using both slice and splice for this one?

pokebeaches
Автор

that was very useful. Thank you for that. Keep up the good work!!

EnglischLernen
Автор

Always struggling how to start it how do u know what to start it with

paulfanning
Автор

The use of the spread operator is what im missing here

BkedBean
Автор

came back here after Understand the Hazards of Using Imperative Code.

brucebergkamp
Автор

Ok, but aren't we supposed to use methods slice AND splice? and you used only slice method 😉
1. function frankenSplice(arr1, arr2, n) {

2. let result = arr2.slice();
3. result.splice(n, 0, ...arr1);
4. return result;
}

L2. // Here we use the slice method to create a copy of arr2, so we make mutations on this copy and not on the original arr2 - arr2 will remain the same //
L3. // Here we use the splice method on the copied arr2 to insert the whole (method spread) arr1 at index n, and since we don't delete any elements from the copied arr2 we assign 0 as the second parameter //

green-jinkots