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.
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.
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.
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;
}