Skip to content

Instantly share code, notes, and snippets.

@jwo

jwo/spoon.js

Created Jun 7, 2017
Embed
What would you like to do?
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