Skip to content

Instantly share code, notes, and snippets.

@nobitagit
Created September 4, 2016 16:05
Show Gist options
  • Save nobitagit/81844445e81047eb137048996fc14b5d to your computer and use it in GitHub Desktop.
Save nobitagit/81844445e81047eb137048996fc14b5d to your computer and use it in GitHub Desktop.
find scales
let values = [{
nation: 'USA',
value: 150
}, {
nation: 'EU',
value: 75
}, {
nation: 'China',
value: 53
}];
let nums = values.map(num => num.value);
/*
* the upper is the list maximum value
*/
function formula (list, scale = 100) {
let maxVal = Math.max.apply(null, list);
return list.map(item => {
return item / maxVal * scale;
});
}
/*
* the upper bound is the sum of all the elements in the list
*/
function formula2 (list, scale = 100) {
let maxVal = list.reduce((a, b) => a + b);
return list.map(item => {
return item / maxVal * scale;
})
}
console.log(formula(nums));
console.log(formula2(nums));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment