So I wonder if there is some kind of glitch that causes the tests to run inconsistently.
In the JavaScript course we have a problem called “drop it.”
I wrote a solution that I considered very easy to read. And it failed for the function testing n > 0
even though the test argument was 1. Now, I think 1 > 0
. One of the sample solutions at the hint page was almost identical to my proposed solution. So I tried the sample solution at the hint page with console print statements to see what that particular function would do. It looks to me like it skipped the n > 0
case entirely.
Appendix: Here is the code, heavily commented to indicate the behavior:
function dropElements(arr, func) {
//https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/drop-it
/**
for(let i = 0; i<arr.length;i++){
let element = arr[i];
console.log(arr+' element at '+i+' is '+element);
let testBoolean = func(i);
console.log(func + " returned "+testBoolean+" for "+element+" at index "+i);
if (!testBoolean){
console.log('arr was '+arr);
console.log(func+' returned false for '+element);
arr.shift();
console.log('now arr is '+arr);
} else {
console.log('test was passed, stop removing stuff');
break;
}
}
*/
//Following is "console logged" version of solution 3 from:
//https://forum.freecodecamp.org/t/freecodecamp-challenge-guide-drop-it/16010
let originalLen = arr.length;
console.log("start with "+func);
for (let i = 0; i < originalLen; i++) {
if (func(arr[0])) {
break;
} else {
console.log('arr was '+arr);
console.log(func+' returned false for '+arr[0]);
arr.shift();
console.log('now arr is '+arr);
}
}
return arr;
return arr;
}
//weird error:
/**
* function (n) {
return n > 0;
} returned false for 1
*/
/**
- Drop it
Given the array arr, iterate through and remove each element starting from the first element (the 0 index) until the function func returns true when the iterated element is passed through it.
Then return the rest of the array once the condition is satisfied, otherwise, arr should be returned as an empty array.
*
*/
/** Testing code seemed to skip the case that broke my earlier version!
*
start with function (n) {
return n === 1;
}
arr was 0,1,0,1
function (n) {
return n === 1;
} returned false for 0
now arr is 1,0,1
start with function (n) {
return n > 0;
}
start with function (n) {
return n > 5;
}
*
*/
dropElements([1, 2, 3], function(n) {return n < 3; });