# freeCodeCamp Challenge Guide: Drop it

``````My solution:
function dropElements(arr, func) {
var index = arr.map(func).indexOf(true);
return (index === -1? []:arr.slice(index));

}``````
1 Like

Exactlyâalso, âlength.arrâ doesnât do wonders, as I noticed after about an hour. The FCC editor didnât catch it.

``````function dropElements(arr, func) {
while(!func(arr[0])) {
arr.shift();
}
return arr;
}
``````
6 Likes

If provided array is empty, this will cause crashing if there is no condition about array length

1 Like
``````function dropElements(arr, func) {
var newArr = [];
for (var i in arr) {
if (func(arr[i])) {
newArr = arr.slice(i);
break;
}
}
if (newArr.length === arr.length) {
newArr.filter(func);
}
return newArr;
}
``````

Nice, I did the exact same thing, only that I use an other âfor loopâ:

``````function dropElements(arr, func) {
for (var e in arr){
if (func(arr[e])) return arr.slice(e);
}
return [];
}``````
2 Likes

Why does running `dropElements([1, 2, 3, 4], function(n) {return n > 5;});` returns `[ 3, 4 ]` when its supposed to return `[ ]` for following code?

It looks like `arr.shift()` removes the element which shortens the length of the array. So using arr.length in the for loop will not get calculated correctly. So thats why the original solution stores `arr.length` as `times` so that itâs consistent across all loop iterations and not get affected by changes from `shift()`.

By why does it still return `[ 3, 4 ]` versus some other number or empty array? Is it because the counter in the for loop is incrementing while the array length is getting reduced? But if we donât increment the for loop, itâll be an infinite loop?

``````function dropElements(arr, func) {
for (var i = 0; i < arr.length; i++) {
// if (func(arr[i])) {
if (func(arr[0]))
break;
else
arr.shift();
}
console.log(arr);
return arr;
}``````

In this case with each iteration you are shortening the arr.length with shift() so in some conditions it wont go through all the elements of arr till the func fails.
That is way in this case you have to store the value of the arr.length to an other variable.

3 Likes

this doesnât work when trying to plug the following info:
[1, 2, 3, 4], function(n) {return n > 5;}

Doesnât work when you try to validate :
[1, 2, 3, 4], function(n) {return n > 5;}

This challenge seems to be bugged for me. It appears to have diffrent results even though algorithms I used are excactly the same. The only difference is loop type.

The first my attempt looked like this. It passes all tests, except this one. In thi case it returns `[0, 1, 0, 1]`. I donât really know why it thinks, that 0 is equal to 1.

``````    function dropElements(arr, func) {
while (arr[0])
{
if (func(arr[0]))
{
return arr;
}
else
{
console.log("removing: "+arr[0]);
arr.shift();
}
}
return arr;
}

dropElements([0, 1, 0, 1], function(n) {return n === 1;});
``````

Second attempt looks like this and as you can see the only difference is the loop type. It works as it should and returns `[1, 0, 1]`.

``````    function dropElements(arr, func) {
for (var i = 0; i===i; i=i)
{
if (func(arr[i]))
{
return arr;
}
else
{
console.log("removing: "+arr[i]);
arr.shift();
}
}
return arr;
}

dropElements([0, 1, 0, 1], function(n) {return n === 1;});
``````

Is there bug or is it something wrong with me?

1 Like

While arr[0] what? exists? Is true? Iâm not sure how that should be interpreted. But I just recently discovered

while (!func(arr[0])){

but mine froze up on CodePen and passed on FreeCodeCamp, so I was curious if you tracked yourâs downâŚ mine seems to be related to

dropElements([1, 2, 3, 4], function(n) {return n > 5;}) should return [].

but as long as I comment that out on Code Pen, it still passes at FCC

Hereâs my solutionâŚ Good enough?

``````function dropElements(arr, func) {
// Drop them elements.
var i = 0;
while(!func(arr[i])){
arr.shift();
}
return arr;
}
dropElements([0, 1, 0, 1], function(n) {return n === 1;});
``````
1 Like

Sliced Array:

``````function dropElements(arr, func) {
// Drop them elements.
var flag = true;
while(arr.length !== 0) {
flag = func(arr[0]);
if(flag === false) {
arr = arr.slice(1);
}
else if(flag === true) {
return arr;
}
}
return arr;
}

``````

my solution:

``````function dropElements(arr, func) {
for (var i=0;i<arr.length;i++) {
var newArr = arr.slice(i,i+1); // check element by element
if(newArr.filter(func).length > 0) { // if it's return true
return arr.slice(i,arr.length); // give me the remaining array
}  else if (i==arr.length-1) { // check if we arrive to the last value without any true
return [];
}
}
}``````

Here is my strange solution for that challenge

``````function dropElements(arr, func) {

var i = 0;

while(i < arr.length){
var n = arr[i];

if(func(n) === false){
arr.shift();
i = 0;
}

else{
return arr.slice(0);
}

}
return arr;
}

dropElements([0, 1, 0, 1], function(n) {return n === 1;});

``````
1 Like

Here is my solution. I am not sure how efficient it is compared to the other solutions here.

``````function dropElements(arr, func) {
var check = arr.filter(func);
var begin = check[0];
return check.length > 0 ? arr.slice(arr.indexOf(begin)) : check;
}``````
1 Like

my solution:
function dropElements(arr, func) {
// Drop them elements.
let index = arr.indexOf(arr.filter(func)[0]); /?/
if (index === -1)
return [];
return arr.slice(index);
}

1 Like
``````function dropElements(arr, func) {
// Map the array to return an array of true or false values for each element
var mapins = arr.map(func);

//locate the index of the first true value
var loc = mapins.indexOf(true);

var arrayFrom1stTrueVal;

//if none of the elements return true in the 'mapins' array
if ( loc < 0){
return [];  //return an empty array
}

//else return the array from the location of the first true value
arrayFrom1stTrueVal = arr.slice(loc);

return arrayFrom1stTrueVal;
}

dropElements([1, 2, 3], function(n) {return n < 3; });
``````

Hereâs my noob solution