Created
September 4, 2016 16:05
-
-
Save nobitagit/81844445e81047eb137048996fc14b5d to your computer and use it in GitHub Desktop.
find scales
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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