Skip to content

Instantly share code, notes, and snippets.

@andrIvash
Created July 28, 2018 09:03
Show Gist options
  • Select an option

  • Save andrIvash/c0069defb735efbdf66a192f0be29189 to your computer and use it in GitHub Desktop.

Select an option

Save andrIvash/c0069defb735efbdf66a192f0be29189 to your computer and use it in GitHub Desktop.
Нормирование массива до указанного диапазона
//y[i]=(x[i]-min)/(max-min)*(b-a)+a;
function normalize (arr, a, b) {
const result = [];
const max = Math.max(...arr);
const min = Math.min(...arr);
arr.forEach(elem => {
result.push(+((elem-min)/(max-min)*(b-a)+a).toFixed(2));
})
return result;
}
console.log(normalize([0,1.5,5.6,3.2,4,2.5,10], 0, 3)); // from 0 to 3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment