Skip to content

Instantly share code, notes, and snippets.

@gorork
Last active August 29, 2015 14:10
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 gorork/fccd1222969f70b2b9e2 to your computer and use it in GitHub Desktop.
Save gorork/fccd1222969f70b2b9e2 to your computer and use it in GitHub Desktop.
P2: JavaScript Challenge #2
var moonWalkers = [
"Neil Armstrong",
"Buzz Aldrin",
"Pete Conrad",
"Alan Bean",
"Alan Shepard",
"Edgar Mitchell",
"David Scott",
"James Irwin",
"John Young",
"Charles Duke",
"Eugene Cernan",
"Harrison Schmitt"
];
function alphabetizer(names) {
var namesLength = names.length; // To avoid multiple calculations in for-loop
var sortedNames = []; // New local array to save results of for-loop
for (var i = 0; i < namesLength; i++) { // Iterate through array to modify each name
sortedNames.push( names[i].split(' ').reverse().join(', ') );
}
return sortedNames.sort(); // Return alphabetically sorted array
}
// Try logging your results to test your code!
console.log(alphabetizer(moonWalkers));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment