Print all even numbers from array?

:upside_down_face: You’ve got a function, which is dead easy to test. But instead of testing the function itself, which is the sensible thing to do, it creates a fake browser, then console logs the result of the function to the fake browser then does some other console log stuff. Also, as far as I can recall there is an assertion function for mocha called deepEquals which is used to check arrays and objects, that forEach is redundant. Urrgh. Just on train so have to cut this short for the minute, I think it’s possibly easy fix to make it work but need a careful look at it

Thanks, I appreciate it. It’s annoying as all hell when the function works, but it doesn’t pass.

Also, they wanted me to console.log the answer instead of return, but it only says “expected 1 to equal 3”


Maybe it has to do something with these directions:

// For example: question0([ 5, 20, 11, 42, 2, 19 ]) >> [ 20, 42, 2 ]

And I’m getting:
assert.equal(console.log.callCount, expected.length)
expected.forEach(num => assert.isTrue(console.log.calledWith(num)))

Sorry, I didn’t understand you previously. Any ideas?

Answer turned out to be that they just wanted me to console log every time. This passed:

function question0 (array){
  for (let i = 0; i < array.length; i++){
    if (array[i] % 2 === 0){

Sorry, I meant to reply sooner: the expected 0 to equal 3 refers to the first assertion. callCount is from the Sinon library used here (the one I said was a fake browser - that’s not quite what it is really, but close enough). The test doesn’t actualpy test the function properly, it just counts how often console log is being called, so adding that console logs in makes the test go green

It seems absolutely crackers: if that’s the only test, I think you could pass it by just having a function that loops three times and console logs each time, I fail to see how this tests the function at all (the function is there, why not just test the function???).

It just baffles me that there is this much ceremony around something so basic, what’s its doing is counter to the instructions :confused:

Two years later…really?

1 Like