Print all even numbers from array?

{
  "name": "js-loops-and-conditionals-assessment",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "./node_modules/.bin/mocha"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "chai": "^3.5.0",
    "mocha": "^3.2.0",
    "mocha-sinon": "^1.1.6",
    "sinon": "^1.17.7"
  }
}

Yes, I understand thanks. Just learning Git now, so I’m still a noobie. Always gonna be.

It’s nothing to do with git, you need the file where the test for that function is defined

I know it has nothing to do with Git lol. I’m telling you with conjunction of this problem and the fact that it requires me to use Git (which I am just learning now), it complicated it a bit for me.

What I’m getting at is: what is the test for that function? I can see everything is running fine, but the test expects some return value to match 3 for that function, which does not seem to match the instructions. The test file has to be in a folder right next to the file you’re working on

Maybe this is it?

const chai = require('chai')
const assert = chai.assert
const main = require('../src/main')

// Listen for console.log statements
require('mocha-sinon')
function stubFn () { this.sinon.stub(console, 'log') }

describe('main', () => {

  beforeEach(stubFn)

  it('question0', () => {
    var input = [ 5, 20, 11, 42, 2, 19 ]
    var expected = [ 20, 42, 2 ]
    main.question0(input)

    assert.equal(console.log.callCount, expected.length)
    expected.forEach(num => assert.isTrue(console.log.calledWith(num)))
  })

Jesus Christ that’s a horrible test

LOL why is that? Hahahahahaha

: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”

EDIT:

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){
      console.log(array[i]);
    }
  }
}

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