HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavascriptMinor

Javascript FizzBuzz and Behavior-Driven Development Using Mocha and Chai

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
javascriptbehaviorfizzbuzzdevelopmentusingmochaanddrivenchai

Problem

I am currently getting closely acquainted with Behavior-Driven Development. Could someone tell me how I am doing with the Fizzbuzz program below? I am interested in both improving the JavaScript code and the BDD prowess. Any help is appreciated.

fizzbuzz.js

module.exports = function(word, value){   
    function sortNumber(a,b) {
        return a - b;
    }
    
    numbers = function(divisor, max){
        var result = []
        for(i=1;i<=max;i++){
            if ((i % divisor) === 0)
                result.push(i)
        }
        return result
    }
    
    if (value <= 0)
        return ['error','nonpositive']
        
    else {
        switch(word) {
            case 'fizz':
                return numbers(3, value)
            case 'buzz':
                return numbers(5,value)
            case 'fizzbuzz':
                return numbers(3,value).concat(numbers(5,value)).filter(function(elem,index,self){
                        return index == self.indexOf(elem)
                    }).sort(sortNumber)
            default:
                return ['error','wrongword']
        }
    }
};


test/fizzbuzz-spec.js:

```
var chai = require('chai');
expect = chai.expect,
fizzbuzz = require('../lib/fizzbuzz');

describe('A fizzbuzz', function(){
describe('will return an error for invalid words: ', function(){
it('like fuss', function(){
expect(fizzbuzz('fuss', 10)).to.be.deep.equal(['error','wrongword']);
});
it('like biss', function(){
expect(fizzbuzz('biss', 10)).to.be.deep.equal(['error','wrongword']);
});
});

describe('will return an error when non-positive integer is passed', function(){
it('like 0', function(){
expect(fizzbuzz('buzz', 0)).to.be.deep.equal(['error','nonpositive']);
})
it('like -2', function(){
expect(fizzbuzz('buzz', -2)).to.be.deep.equal(['error','nonpositive']);
})
})

describe('wil

Solution

Handling invalid input

Instead of returning an array when inputs are invalid, throw exceptions and use throw to detect it in the tests.
Avoid else when previous blocks have return statements

The else can be eliminated since the block evaluated when value <= 0 has a return statement (or would throw an exception). Additionally, the switch statement can be simplified to a few if statements (for an example see this answer to a related question).
Don't forget to set scopes

While the code is in a module, it is best to set the scope of variables and functions - e.g. numbers and i in the loop within numbers should be prefixed by the keywords const and let, respectively, to avoid accidental re-assignment/confusion.

In that same vein, the first few lines of the test are setting variables:

var chai = require('chai');
    expect = chai.expect,
    fizzbuzz = require('../lib/fizzbuzz');


Perhaps you meant to add a comma instead of semi-colon after require('chai')? It would be simpler just to have each variable on its own line:

const chai = require('chai');
const expect = chai.expect;
const fizzbuzz = require('../lib/fizzbuzz');


Actually instead of importing chai just to use expect from it on a separate line, use object-destructuring:

const { expect } = require('chai');
const fizzbuzz = require('../lib/fizzbuzz');

Code Snippets

var chai = require('chai');
    expect = chai.expect,
    fizzbuzz = require('../lib/fizzbuzz');
const chai = require('chai');
const expect = chai.expect;
const fizzbuzz = require('../lib/fizzbuzz');
const { expect } = require('chai');
const fizzbuzz = require('../lib/fizzbuzz');

Context

StackExchange Code Review Q#162939, answer score: 3

Revisions (0)

No revisions yet.