Last active
August 29, 2015 14:10
-
-
Save gorork/fccd1222969f70b2b9e2 to your computer and use it in GitHub Desktop.
P2: JavaScript Challenge #2
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
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