Very weird testing issue with "drop it"

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; });

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (’).

1 Like

what are you testing here tho? is it the array element?

1 Like

testBoolean would return “true” if func(i) returned true.

But I see what you mean. I would expect this to be func(arr[i]) or func(element) – that is probably what caused the one test to fail.

I know it passed all but one test with the earlier version, and if that func(i) was in there when that happened, those passed tests may have been accidental.

Thanks for spotting this.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.