Algorithm Drop It - thelastmile/FreeCodeCamp GitHub Wiki
Algorithm Drop It
:triangular_flag_on_post: Remember to use Read-Search-Ask
if you get stuck. Try to pair program :busts_in_silhouette: and write your own code :pencil:
:checkered_flag: Problem Explanation:
Basically while the second argument is not true, you will have to remove the first element from the left of the array that was passed as the first argument.
Relevant Links
:speech_balloon: Hint: 1
You can use Array.prototype.shift()
or filter that you should be more familiar with to solve this problem in a few lines of code.
try to solve the problem now
:speech_balloon: Hint: 2
Shift returns the element that was removed which we don't really need, all we need is the modified array that is left.
try to solve the problem now
:speech_balloon: Hint: 3
If you still can't figure out how to solve it with shift, then try solving it with filter, and check how filter works, if you become familiar with it, then you can make the code with shift.
try to solve the problem now
Spoiler Alert!
Solution ahead!
:beginner: Basic Code Solution:
function dropElements(arr, func) {
// drop them elements.
var times = arr.length;
for (var i = 0; i < times; i++) {
if (func(arr[0])) {
break;
} else {
arr.shift();
}
}
return arr;
}
// test here
dropElements([1, 2, 3, 4], function(n) {return n >= 3;})
:rocket: Run Code
Code Explanation:
- Create a for loop to check each element.
- Then check for the function given if true then stop, otherwise remove that element.
- return the array.
Relevant Links
:sunflower: Intermediate Code Solution:
function dropElements(arr, func) {
return arr.slice(arr.findIndex(func) >= 0 ? arr.findIndex(func): arr.length, arr.length);
}
// test here
dropElements([1, 2, 3, 4], function(n) {return n >= 3;});
:rocket: Run Code
Code Explanation:
- Use ES6
findIndex()
function to find the index of the element that passes the condition - Slice the array from the found index until the end
- There is one edge case! if the condition is not met against any of the elements 'findIndex' will return
-1
which messes up the input toslice()
. In this case use a simple conditional operator to returnfalse
instead of-1
. And the ternary operator (?:) returns the found index of required elements when the condition istrue
, and the length of the array otherwise so that the return value is an empty array as is instructed.
Relevant Links
:rotating_light: Advanced Code Solution:
function dropElements(arr, func) {
while(arr.length > 0 && !func(arr[0])) {
arr.shift();
}
return arr;
}
// test here
dropElements([1, 2, 3, 4], function(n) {return n >= 3;});
:rocket: Run Code
Code Explanation
- Use a while loop with
Array.prototype.shift()
to continue checking and dropping the first element of the array until the function returns true. It also makes sure the array is not empty first to avoid infinite loops. - Return the filtered array.
Relevant Links
:trophy: 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 @Rafase282 @sabahang for your help with Algorithm: Drop it
:clipboard: NOTES FOR CONTRIBUTIONS:
- :warning: 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. :traffic_light:
- Please add your username only if you have added any relevant main contents. (:warning: DO NOT remove any existing usernames)
See :point_right:
Wiki Challenge Solution Template
for reference.