Skip to content

Instantly share code, notes, and snippets.

@achisholm
Created May 13, 2014 07:31
Show Gist options
  • Save achisholm/6ccdd02478ad2e46e02d to your computer and use it in GitHub Desktop.
Save achisholm/6ccdd02478ad2e46e02d to your computer and use it in GitHub Desktop.
Exercise in summing
// 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