# SOLVED:Basic Algorithm Scripting: Slice and Splice2

Sorry to bother

function frankenSplice(arr1, arr2, n) {
let arr3 = arr2 + arr2.splice(n, 0, arr1);
console.log(arr1, arr2, arr3);
return arr3;
}

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

can someone help me to understand why my code is wrong ?

link to the challenge? way faster to look at the problem than search it from the curriculm (donât delete it)
also, you wrote

so i think (but donât know cuz i donât know the challenge) that the first parameter is supposed to be 0 then n

@harel_avv
itâs not a challenge is part of the study

you need to have a proper understanding of splice for this, read the documentation (from develpoer mozilla/ MDN):

### Return value:

An array containing the deleted elements.

If only one element is removed, an array of one element is returned.

If no elements are removed, an empty array is returned.

note that splice already CHANGES the array when its called, so you need to just call splice without any assignments

if you see my console (in the pic) and the request of the test, you can see my answer is right.
i am curious to understand why itâs not.

if yourâe curious, hereâs what I got when logging youre function for
[1, 2, 3], [4, 5], 1 :

[ 1, 2, 3 ] [ 4, [ 1, 2, 3 ], 5 ] 4,1,2,3,5
4,1,2,3,5

i donât think itâs the right answer

41235 itâs right the answer of the test.
the other are just console.log to check my work.
and if you read my post i have already posted the result in the pic given.

no, âthe othersâ are a part of a single console.log statement, the function return value is that, not just â41235â, and also itâs a string (not an array as the tests require), I solved this challenge now within half a minute, so can you please use my advice? because I know what Iâm talking about

function frankenSplice(arr1, arr2, n) {
let arr3 = arr2 + arr2.splice(n, 0, arr1);
console.log(arr3);
return arr3;
}

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

if i only console.log arr3 here is the answer.

There are several points to consider:
1: the + operator is not really doing what you think itâs doing.
In this case, the fact that your operators are not number means that the JS engine attempts to convert the values into string and concatenate them.
meaning that your arr3 is a string and not an array.

let a1 = [1,2,3]
let a2 = [4,5,6]

typeof (a1 + a2) // string

2 - Splice change the Array in place, meaning that arr2 wonât be the same after, and the challenge specifically ask you to not modify the arguments passed.

3 - arr2.splice(n, 0, arr1); you are effectively inserting an array inside arr2, meaning that the final result will be an array inside an array (a 2d array)
like

[1,2, [3,4,5],6]

Which is not the intended result.

Hope it helps

try changing the console.log statement in your function to this console.log(typeof arr3)
it returns âstringâ, NOT an array

@Marmiz
THANK YOU MARMIZ! i was waiting for you
You are always kind, competent and veryâŚsimple in your explanations.

I appreciate your kind words, but,
@harel_avv tried to help as well, and he deserve any praise and recognition as well.
Reading at his comments (they) said pretty much the exact same things

Also please remember that many people here is hanging around âvolunteeringâ their free time just to help others, and thatâs a BIG enough reason to be thankful to everybody regardlessâŚ

you are right and sure harel said the same things but not in the same way.
as i understand that you all here to help us free, you understand that i need an explanation to improve. a simple explanation.
as you can see the first answer of Harel, he was not explaining my fail. he just told me to study splice.

Now, I donât want to digress too much as it is off-topic, but with hope that this may help you in your future endeavor @marcocbnet.

50% of developing software is making research and reading tons of documentation. So you have to get used to read - re read and read over and over again documentationâŚ because thatâs what you will do.
Moreover you will learn a great deal more if you do your own research.

Learning to learn, and to search for answer is a great asset to have as a software developer, that in general will be required for any job: none expects you to know everything, but companies/coworkers will expect you to find answers on your own.

Thereâs a difference in approaching a problem asking
âwhy x doesnât workâ
compared to
"why my âxâ has this precise behavior when according to âyâ it should be âzâ "?

1 Like

i do not reply anymore. i apologize to Harel if i did not catch his help.
thank you

1 Like

Sometimes âread the docsâ is the best advice, or âthe soleâ advice you will get, so itâs something to learn to live with

Just for context at my workplace we have a small chatbot that link to internal documentation, and often in the team chat you read someone asking
âhey do you remember [thing?]âŚâ
which is often replied with
âI think itâs [link to doc]â

and thatâs all you get

1 Like

We should introduce a forum bot with zero social skills that just answers every question with a link to the docs, signing each post with âwelcome to your life as devâ

1 Like

that would be the less newbie friendly thing ever

butâŚ @nhcarrigan do you have space for an extra feature for your bot?

This time i donât agree with you. why did you explain everything step by step instead of tell me to study? Because i need to understand before study. And thatâs why your help is so important for me. you didnât give me the solution, you gave me the comprehension to understand the problem.
when you and you collegue share the same document is because you are on the same level. if you share your library with me i couldnât understand a single line of code, even if i study a month