Algorithm Slasher Flick

🚩 Remember to use Read-Search-Ask if you get stuck. Try to pair program πŸ‘₯ and write your own code πŸ“

🏁 Problem Explanation:

For example: slasher([1, 2, 3], 2); must return [3].

Relevant Links

πŸ’¬ Hint: 1

  • We need only the remaining part of an array, so we can just remove what we don’t.

try to solve the problem now

πŸ’¬ Hint: 2

  • splice() function can be used.

try to solve the problem now

πŸ’¬ Hint: 3

  • slice() function can be used.

try to solve the problem now

Spoiler Alert!

687474703a2f2f7777772e796f75726472756d2e636f6d2f796f75726472756d2f696d616765732f323030372f31302f31302f7265645f7761726e696e675f7369676e5f322e676966.gif

Solution ahead!

πŸ”° Basic Code Solution:

function slasher(arr, howMany) {
  // remove the head
  arr.splice(0, howMany);
  // return the remaining or the tail
  return arr;
}
slasher([1, 2, 3], 2);

πŸš€ Run Code

Code Explanation:

  • This solution uses the splice() function.
  • First argument, arr is the array to be modified.
  • Second argument, howMany is the number of elements to be removed starting with arr[0].
  • Modify the array with splice() and return it.

🌻 Intermediate Code Solution:

function slasher(arr, howMany) {

  // Return string after the amount chopped off.
  return arr.slice(howMany);

}

πŸš€ Run Code

Code Explanation:

  • This solution uses the slice() function.
  • The argument howMany is the number of elements to be removed starting with arr[0].
  • Modify the array with slice() and return it.

πŸ† Credits:

If you found this page useful, you may say thanks to the contributors by copying and pasting the following line in the main chat:

Thanks @clint74 @Rafase282 @M-Bowe for your help with Algorithm: Slasher Flick

πŸ“‹ NOTES FOR CONTRIBUTIONS:

  • ⚠ DO NOT add solutions that are similar to any existing solutions. If you think it is similar but better, then try to merge (or replace) the existing similar solution.
  • Add an explanation of your solution.
  • Categorize the solution in one of the following categories β€” Basic, Intermediate and Advanced. πŸš₯
  • Please add your username only if you have added any relevant main contents. (⚠ DO NOT remove any existing usernames)

See πŸ‘‰ Wiki Challenge Solution Template for reference.