Created
May 10, 2021 14:34
-
-
Save RahmatSaeedi/7ed7db6d82e8aa7d0ae130d4dd2d1737 to your computer and use it in GitHub Desktop.
CodeSignal - Arcade - Intro - JS - absoluteValuesSumMinimization
This file contains 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
function absoluteValuesSumMinimization(a) { | |
let sum = a.map( i => distanceCalculator(a, i)); | |
let sumMin = sum.slice().sort((a,b) => a-b)[0]; | |
return a[sum.indexOf(sumMin)]; | |
} | |
function distanceCalculator(a, val){ | |
return a.map(i => Math.abs(i-val)).reduce((sum, num) => sum + num); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment