Lodash project - test error

I’m working on https://www.codecademy.com/paths/full-stack-engineer-career-path/tracks/fscp-javascript-syntax-part-ii/modules/fecp-practice-javascript-syntax-arrays-loops-objects-iterators/projects/lodash. I’ve gotten down to the dropWhile() section.

My drop() code passed its test, but now when I try to test the dropWhile() function it refers to an error in the drop() method. I’ve watched the walkthrough video and I did the same thing that the instructor did, so I don’t understand what happened. Help, please?

The error is:
1 - .dropWhile() is defined - Passed!
/home/ccuser/workspace/underscore-javascript-capstone/
.js:60
let droppedArray = array.slice(n, array.length);
^
TypeError: Cannot read property ‘slice’ of undefined
at Object.drop (/home/ccuser/workspace/underscore-javascript-capstone/.js:60:29)
at Object.dropWhile (/home/ccuser/workspace/underscore-javascript-capstone/
.js:68:27)
at Object. (/home/ccuser/workspace/underscore-javascript-capstone/test/drop-while.js:17:135)
at Module._compile (module.js:571:32)
at Object.Module._extensions…js (module.js:580:10)
at Module.load (module.js:488:32)
at tryModuleLoad (module.js:447:12)
at Function.Module._load (module.js:439:3)
at Module.runMain (module.js:605:10)
at run (bootstrap_node.js:427:7)

drop(array, n) {
  if(n === undefined) {
    n = 1;
  } 
    let droppedArray = array.slice(n, array.length);
    return droppedArray;
},
dropWhile(array, predicate) {
  const cb = (element, index) => {
    return !predicate(element, index, array);
  };
  let dropNumber = array.findIndex(cb);
  let droppedArray = this.drop(array.dropNumber);
  return droppedArray;
}
};

Disclaimer: I haven’t done these so I might be missing some context…

If i’m following this correctly

let droppedArray = this.drop(array.dropNumber); is inside of dropWhile()

so whatever array is being passed is coming from whatever is passed to dropWhile() on to drop() is failing at array.slice() because it’s undefined…

Is it enough to add a base case for array being undefined like you do for n (inside of drop()…? (I guess not if it’s not in the instructions, I’ll leave this in because I love wrong approaches haha).

Alternatively this line could be failling: let dropNumber = array.findIndex(cb);, so then it’d be to check the logic for the cb declaration…

What does the cb declaration return? A boolean? So dropNumber = array.findIndex(someBoolean)?

Hopefully some of these musings help…

Is array.dropNumber in this.drop(array.dropNumber) supposed to be an array? It seems like drop() takes arrays as arguments (with n being optional)…, maybe this.drop(array, array.dropNumber)??

1 Like

The array that’s being passed into dropWhile() comes from the test suite for the project. And I can’t tell why it’s failing.

I took your suggestions and changed the drop() method to:

drop(array, n){
  if(n === undefined){
    n = 1;
  } if(array === undefined) {
    array = [];
  }
    let droppedArray = array.slice(n, array.length);
    return droppedArray;
},

It still passes and the dropWhile() method gets a little further in the tests, so you’re onto something!

Now it says the dropWhile() method isn’t passing the final test:
*3 - Drops elements until predicate function returns falsy - Failed: _.dropWhile([1, 2, 0, 4], indexIsSmallerThanElement returned [ ] instead of [ 0, 4 ].
*

Did you try this?:

let droppedArray = this.drop(array, array.dropNumber);

I would insert console.log statements that check for the type of the variables as they trickle down the code just to double-check. The base case idea was a red-herring I think.

1 Like

Did you change that period to a comma?

2 Likes

That was it! Thank you, both @mtf and @toastedpitabread!

1 Like