Created
June 7, 2017 16:15
-
-
Save jwo/b27de87c6cf602888032168286d9171c to your computer and use it in GitHub Desktop.
Such a great super great awesome example of nested for loops
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
function properCase(sentence){ | |
const words = sentence.split(' '); | |
let newWords = [] | |
// Start the big spoon | |
for(let i = 0; i < words.length; i++){ | |
const word = words[i]; | |
const letters = word.split(''); | |
let newLetters = []; | |
// Start the little spoon, once per bigSpoon loop | |
for(let n = 0; n < letters.length; n++){ | |
const letter = letters[n]; | |
if (n === 0){ | |
newLetters.push( letter.toUpperCase() ) | |
} else { | |
newLetters.push( letter ) | |
} | |
} | |
newWords.push( newLetters.join('') ) | |
} | |
return newWords.join(' ') | |
} | |
console.log( properCase("oh hai I am happy") ) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment