Last Updated: February 25, 2016
·
925
· jbilcke

Array::normalize in CoffeeScript

this function will use the minimum and the maximum values found in the array, to normalize everything between 0 and 1

Array::normalize = ->
  return @ unless @length
  min = max = @[0]
  for i in @
    min = i if i < min
    max = i if i > max
  return @ unless max - min
  for i in [0...@length]
    @[i] = (@[i] - min) / (max - min)
  @

example:

console.log [300,400,150,230,200].normalize()
[ 0.6, 1, 0, 0.32, 0.2 ]