Skip to content

Instantly share code, notes, and snippets.

@vacas
Last active April 2, 2017 04:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vacas/ed0a7ab70b49285a5614a03e14baeb3e to your computer and use it in GitHub Desktop.
Save vacas/ed0a7ab70b49285a5614a03e14baeb3e to your computer and use it in GitHub Desktop.
Coding Challenge for Codetrotters Fellowship - Goal: Function that takes an array of whole numbers and organizes them in the largest possible number together
var arrays = [
[9, 17, 0, 50],
[ 342, 689 ],
[ 43, 5, 12, 99 ],
[ 8, 4, 100 ],
[ 0, 1, 2, 3, 4, 5, 6, 7 ]
];
function createHighestNumber(input, firstDigit, output){
while (input.length > 0) {
for (var j = 0; j < firstDigit.length; j++) {
if (firstDigit[j] == Math.max.apply(null, firstDigit)) {
output += input[j].toString();
input.splice(j, 1);
firstDigit.splice(j, 1);
}
}
}
return output;
}
function highestNumber(array) {
var list = [],
str = '';
for (var i = 0, end = array.length; i < end; i++) {
list.push(array[i].toString()[0]);
}
str = createHighestNumber(array, list, str);
return str;
}
for (var n = 0; n < arrays.length; n++) {
highestNumber(arrays[n]);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment