3.0.0 • Published 8 years ago

greatest-number-in-array v3.0.0

Weekly downloads
3
License
ISC
Repository
github
Last release
8 years ago

Determine which number is the greatest in an array.

Usage

npm install --save greatest-number-in-array
var greatestNumberInArray = require('greatest-number-in-array');

greatestNumberInArray([1, 5, 2, 10]); // 10

Rules

  • If the supplied argument is not an array, throw a TypeError
  • If the array is empty return null.
  • If the number is not a number throw a TypeError (numeric strings are supported, NaN is not)
  • The result is cast

Code

// version 3.0.0

var isNumber = require('is-number');

module.exports = function greatestNumberInArray(array) {
    if (! Array.isArray(array)) {
        throw TypeError('greatest-number-in-array: Can not find greatest number in non array');
    }

    var greatest = null;

    for (var i = 0; i < array.length; i++) {
        if (! isNumber(array[i])) {
            throw TypeError('greatest-number-in-array: Value at index ' + i + ' is not a number');
        }

        var value = Number(array[i]);

        if (greatest === null || value > greatest) {
            greatest = value;
        }
    }

    return greatest;
}
3.0.0

8 years ago

2.0.1

9 years ago

2.0.0

9 years ago

1.0.4

9 years ago

1.0.3

9 years ago

1.0.2

9 years ago

1.0.1

9 years ago

1.0.0

9 years ago