Created
May 13, 2014 07:31
-
-
Save achisholm/6ccdd02478ad2e46e02d to your computer and use it in GitHub Desktop.
Exercise in summing
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
// http://www.codewars.com/kata/52cd0d600707d0abcd0003eb/train/javascript | |
// Test Failed: maximumSum with n>values.length not working as expected | |
var values = [5, 4, 3, 2, 1]; | |
function minimumSum(values, n){ | |
values.sort(); | |
var total = 0; | |
for (var i = 0; i < n; i++) { | |
total += values[i]; | |
} | |
return total; | |
} | |
function maximumSum(values, n) { | |
values.sort().reverse(); | |
var total = 0; | |
for (var i = 0; i < n; i++) { | |
total += values[i]; | |
} | |
return total; | |
} | |
console.log(minimumSum(values, 2)); | |
// should return 1+2 = 3 | |
console.log(maximumSum(values, 3)); | |
// should return 3+4+5 = 12 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment