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

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

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);
    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)

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

Which is not the intended result.

Hope it helps :sparkles:

try changing the console.log statement in your function to this console.log(typeof arr3)
it returns “string”, NOT an array

THANK YOU MARMIZ! i was waiting for you :slight_smile:
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 :slight_smile:

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… :sparkles::rainbow:

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.

That is probably the best advice for your growth.
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 :wink:

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 :sweat_smile::slight_smile:

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” :smiley:

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