Algorithm slasher flick - thelastmile/FreeCodeCamp GitHub Wiki

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 @dakshshah96 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. (:warning: DO NOT remove any existing usernames)

See 👉 Wiki Challenge Solution Template for reference.

⚠️ **GitHub.com Fallback** ⚠️