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 ]
Written by Julian bilcke
Related protips
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Coffeescript
Authors
iam4x
94.17K
ericdfields
63.02K
Related Tags
#coffeescript
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#