# What do you think of my solution? Is using three for loops too messy?

Really enjoying the Basic algorithm scripting section so far. Here is my solution to this challenge. I am wondering whether using three for loops is okay or too messy?
Describe your issue in detail here.

``````  **Your code so far**
``````
``````
function frankenSplice(arr1, arr2, n) {
let sum=arr1.length+arr2.length;
let arr=[];

for(let j=0;j<n;j++){
arr.push(arr2[j]);
}
for(let j=0;j<arr1.length;j++){
arr.push(arr1[j]);
}
for(n;n<arr2.length;n++){
arr.push(arr2[n]);

}
console.log(arr);
return arr;
}

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

User Agent is: `Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.212 Safari/537.36 OPR/76.0.4017.177`

Challenge: Slice and Splice

1 Like

If it works, it works. Though I think array methods are cleaner.

1 Like

Itâ€™s fine for a solution without array methods. With array methods, itâ€™s doable in one line.

``````function frankenSplice(arr1, arr2, n) {
return [...arr2.slice(0, n), ...arr1, ...arr2.slice(n)];
}
``````
2 Likes

I was just checking out your profile and saw your comment on the FCC ruined my life post. The post and some comments were so demotivating about how hard is to find a job after completing all the certifications that is not being able to get a job and all like one guy said in the comments he applied to 200+ jobs and didnâ€™t get any.

Edit: However there are other things that are exciting too like being able to make your own apps, websites and other fun things. So it also motivates a bit too.

2 Likes

An important thing to keep in mind is that you need to balance performance with readability, and I think the logic of your code is easy to understand which is a great thing. I think being able to write highly performant code is something to strive for, but in reality you probably wonâ€™t make that much use out of it because generally speaking you donâ€™t want to write code that will cause a future worker an hours worth of reading just to find out how nifty your algorithm is.

Keeping in line with your style of code I did manage to do it all in one loop, but where you have three loops I have one loop and two conditionals so I wouldnâ€™t say I saved any lines by going that rout, and yours has the added benefit of being easier to understand.

2 Likes

Youâ€™re right.

Here is my updated code. For some reason I guess because of working a lot these past couple of days like staying up till 2-3 am coding, I kind of forgot that Slice and Split has already been taught when it comes to arrays which I did use in the first few/1-2 challenges(I was confusing it with other String methods and other methods like map and all which has not been taught so did not give a think about it). So went back and implemented it on every single one of the scripting challenges where it could be implemeted.

Here is my updated code:

``````function frankenSplice(arr1, arr2, n) {
let arr=arr2.slice(0,n);
arr.push(...arr1);
arr.push(...arr2.slice(n,arr2.length));
console.log(arr);
return arr;
}

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

``````

I saw the solutions after I solved it and this was my favorite one.

``````function frankenSplice(arr1, arr2, n) {
// It's alive. It's alive!
let localArr = arr2.slice();
localArr.splice(n, 0, ...arr1); /*from the nth index delete zero
elements, right there start inserting all elements of arr1, its
like pushing items in the middle*/
return localArr;
}

``````
1 Like

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