Skip to content

Commit

Permalink
[UPDATE] implementation, tests, example
Browse files Browse the repository at this point in the history
  • Loading branch information
Planeshifter committed Feb 22, 2015
1 parent 845a192 commit a2d2b16
Show file tree
Hide file tree
Showing 4 changed files with 119 additions and 16 deletions.
46 changes: 40 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ nanprod
===
[![NPM version][npm-image]][npm-url] [![Build Status][travis-image]][travis-url] [![Coverage Status][coveralls-image]][coveralls-url] [![Dependencies][dependencies-image]][dependencies-url]

> Computes the product over an array of values ignoring any values which are not numeric.
> Computes the product over an array of values ignoring any values which are not numeric.

## Installation
Expand All @@ -16,19 +16,40 @@ For use in the browser, use [browserify](https://github.com/substack/node-browse

## Usage

To use the module,

``` javascript
var foo = require( 'compute-nanprod' );
var nanprod = require( 'compute-nanprod' );
```

#### foo( arr )
#### nanprod( arr )

Computes the product while ignoring non-numeric values.

What does this function do?
``` javascript
var data = [ 1, NaN, 2, NaN, 1 ];

var product = nanprod( data );
// returns 2
```

## Examples

``` javascript
var foo = require( 'compute-nanprod' );
var nanprod = require( 'compute-nanprod' );

var data = new Array( 10 );

for ( var i = 0; i < data.length; i++ ) {
if ( i%5 === 0 ) {
data[ i ] = NaN;
} else {
data[ i ] = Math.random() + 1;
}
}

console.log( nanprod( data ) );

```

To run the example code from the top-level application directory,
Expand All @@ -38,6 +59,19 @@ $ node ./examples/index.js
```


## Notes

The product of an array containing non-numeric values is equal to the product of an equivalent array which contains only the numeric values. Hence,

``` javascript
var d1 = [ 1, NaN, 2, 3, NaN ],
d2 = [ 1, 2, 3 ];

console.log( nanprod( d1 ) === nanprod( d2 ) );
// returns true
```


## Tests

### Unit
Expand Down Expand Up @@ -69,7 +103,7 @@ $ make view-cov
---
## License

[MIT license](http://opensource.org/licenses/MIT).
[MIT license](http://opensource.org/licenses/MIT).


## Copyright
Expand Down
14 changes: 13 additions & 1 deletion examples/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
'use strict';

var module = require( './../lib' );
var nanprod = require( './../lib' );

var data = new Array( 10 );

for ( var i = 0; i < data.length; i++ ) {
if ( i%5 === 0 ) {
data[ i ] = NaN;
} else {
data[ i ] = Math.random() + 1;
}
}

console.log( nanprod( data ) );
31 changes: 25 additions & 6 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
*
*
* DESCRIPTION:
* - Computes the product over an array of values ignoring any values which are not numeric.
* - Computes the product over an array of values ignoring any values which are not numeric.
*
*
* NOTES:
Expand Down Expand Up @@ -35,15 +35,34 @@

// FUNCTIONS //

// NANPROD //
/**
* FUNCTION: foo()
* {{ foo description }}.
* FUNCTION: nanprod( arr )
* Computes the product over an array of values ignoring non-numeric values.
*
* @param {Array} arr - array of values
* @returns {Number} prod
*/
function foo() {
function nanprod( arr ) {
if ( !Array.isArray( arr ) ) {
throw new TypeError( 'nanprod()::invalid input argument. Must provide an array.' );
}
var len = arr.length, p = 1, val;

} // end FUNCTION foo()
for ( var i = 0; i < len; i++ ) {
val = arr[ i ];
if ( typeof val !== 'number' || val !== val ) {
continue;
}
p *= val;
if (p === 0){
break;
}
}
return p;
} // end FUNCTION nanprod()


// EXPORTS //

module.exports = foo;
module.exports = nanprod;
44 changes: 41 additions & 3 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ var // Expectation library:
chai = require( 'chai' ),

// Module to be tested:
lib = require( './../lib' );
nanprod = require( './../lib' );


// VARIABLES //
Expand All @@ -21,9 +21,47 @@ var expect = chai.expect,
describe( 'compute-nanprod', function tests() {

it( 'should export a function', function test() {
expect( lib ).to.be.a( 'function' );
expect( nanprod ).to.be.a( 'function' );
});

it( 'should do something' );
it( 'should throw an error if provided a non-array', function test() {
var values = [
'5',
5,
true,
undefined,
null,
NaN,
function(){},
{}
];

for ( var i = 0; i < values.length; i++ ) {
expect( badValue( values[i] ) ).to.throw( TypeError );
}
function badValue( value ) {
return function() {
nanprod( value );
};
}
});

it( 'should compute the product', function test() {
var data, expected;

data = [ 2, 4, NaN, 5, 3, 8, NaN, 2 ];
expected = 1920;

assert.strictEqual( nanprod( data ), expected );
});

it( 'terminates early if product equals zero', function test() {
var data, expected;

data = [ 2, 4, NaN, 0, 3, 8, NaN, 2 ];
expected = 0;

assert.strictEqual( nanprod( data ), expected );
});

});

0 comments on commit a2d2b16

Please sign in to comment.