Skip to content

Instantly share code, notes, and snippets.

@jwo
Created June 7, 2017 16:15
Show Gist options
  • Save jwo/b27de87c6cf602888032168286d9171c to your computer and use it in GitHub Desktop.
Save jwo/b27de87c6cf602888032168286d9171c to your computer and use it in GitHub Desktop.
Such a great super great awesome example of nested for loops
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