How to write more streamlined code (Basic Algorithm Scripting - Slice and Splice)

Tell us what’s happening:

A bit of background: I am just starting on my coding journey, and have been at it for about a week after having learned only some basic Python a good few years ago. I have gone through the HTML/CSS course, and am now doing the JavaScript work and going through the algorithm scripting section.

See below for some code that I have written.

I have made a habit of trying to supercharge my learning by having a look at the model answers after finding my own answer, and often find that my code is much longer and perhaps not as streamlined. I’ve included a link to the hints page with the solutions to this challenge below for ease.

I tend to use iterative loops instead of functions such as splice. I am not finding that I am unable to solve the problems (in the example below, I believe the code accomplishes the task in all cases and passes all tests), but it seems perhaps too complex.

Part of the reason for this is a lack of familiarity with or knowledge of the existence of these more complex methods. Is this something I should be sitting down and memorising, or is it something that just takes time? Is there anything I should be doing to speed up this process?

And, finally, is this even something that I should be worrying about, or should I just be content if the code solves the problem?

Your code so far

function frankenSplice(arr1, arr2, n) {
  let solutionArray = [];
  let newArray = [...arr2];
  for (let i = 0; i < n; i++) {
    solutionArray.push(arr2[i]);
    newArray.shift();
  };
  solutionArray.push(...arr1);
  solutionArray.push(...newArray);
  return solutionArray;
}

Challenge: Basic Algorithm Scripting - Slice and Splice

Link to the challenge:

Link to solutions:

I can assure you that nobody is writing such sleek solutions on a first try (not encountering similar problems dozen of times in the past), while being beginner. Some of them are more about showing what’s possible, rather than going for readability.

Focusing on making things work is good. That’s what ultimately matters.

After having working solution take another look at your code. See what might be simplified, what looks complicated, etc. If you have some idea how it can be improved - try it. Even if it doesn’t work this time, you will always get something from it.

With more practice and being more familiar with JavaScript this will get easier, so keep it going :slight_smile:

1 Like