Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge remote-tracking branch 'upstream/master' into cumsum

  • Loading branch information...
commit 82c8640044d11b64a2edd962315901ffda96f292 2 parents 3c03592 + f866959
tushar gupta tushargupta51 authored
Showing with 17 additions and 23 deletions.
  1. +3 −8 doc/md/vector.md
  2. +12 −13 src/vector.js
  3. +2 −2 test/vector/mode-test.js
11 doc/md/vector.md
View
@@ -377,13 +377,10 @@ And the two can be combined.
**mode( array )**
Return the mode of a vector.
-If there are multiple modes then `mode()` will return false.
-
-**Note:** There is a feature request to return an array if multiple modes exist
-([Issue 44](https://github.com/jstat/jstat/issues/44 "")).
+If there are multiple modes then `mode()` will return all of them.
jStat.mode([1,2,2,3]) === 2
- jStat.mode([1,2,3]) === false
+ jStat.mode([1,2,3]) === [1,2,3]
**fn.mode( [bool][,callback] )**
@@ -400,9 +397,7 @@ If callback is passed then will pass result as first argument.
If pass boolean true as first argument, then return mode of the matrix.
-**Note:** See [BUG #50](https://github.com/jstat/jstat/issues/50)
-
- jStat([[1,2],[1,2]]).mode( true ) === false
+ jStat([[1,2],[1,2]]).mode( true ) === [[1,2],[1,2]]
And the two can be combined.
25 src/vector.js
View
@@ -121,7 +121,7 @@ jStat.extend({
},
// mode of an array
- // if there are multiple modes of an array, just returns false
+ // if there are multiple modes of an array, return all of them
// is this the appropriate way of handling it?
mode : function( arr ) {
var arrLen = arr.length,
@@ -130,28 +130,27 @@ jStat.extend({
maxCount = 0,
numMaxCount = 0,
i = 0,
- maxNum;
+ mode_arr = [];
for ( ; i < arrLen; i++ ) {
if ( _arr[ i ] === _arr[ i + 1 ] ) {
count++;
} else {
if ( count > maxCount ) {
- maxNum = _arr[i];
+ mode_arr = [ _arr[i] ];
maxCount = count;
- count = 1;
numMaxCount = 0;
- } else {
- // are there multiple max counts
- if ( count === maxCount ) {
- numMaxCount++;
- // count is less than max count, so reset values
- } else {
- count = 1;
- }
+ }
+ // are there multiple max counts
+ else if ( count === maxCount ) {
+ mode_arr.push(_arr[i]);
+ numMaxCount++;
}
+
+ // resetting count for new value in array
+ count = 1;
}
}
- return ( numMaxCount === 0 ) ? maxNum : false;
+ return ( numMaxCount === 0 ) ? mode_arr[0] : mode_arr;
},
// range of an array
4 test/vector/mode-test.js
View
@@ -16,7 +16,7 @@ suite.addBatch({
assert.equal( jStat([1,2,3,3]).mode(), 3 );
},
'mode matrix cols' : function( jStat ) {
- assert.deepEqual( jStat([[1,2],[1,4]]).mode(), [1,false]);
+ assert.deepEqual( jStat([[1,2],[1,4]]).mode(), [1,[2,4]]);
}
},
'fn.mode vector' : {
@@ -32,7 +32,7 @@ suite.addBatch({
jStat([[1,2],[1,4]]).mode( this.callback );
},
'mode matrix cols callback' : function( val, stat ) {
- assert.deepEqual( val, [1,false] );
+ assert.deepEqual( val, [1,[2,4]] );
}
}
});
Please sign in to comment.
Something went wrong with that request. Please try again.