Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // imperative, state mutation, no separation of side effects
- (function latinize(phrase){
- let phraseArray = phrase.split(' ');
- const reducer = phraseArray.reduce((accumulator, currentValue) => {
- let firstLetter = currentValue.substring(0,1);
- let rest = currentValue.substring(1);
- let latin = 'ay ';
- accumulator += rest + firstLetter + latin;
- return accumulator
- }, (''))
- console.log(reducer)
- })('quick brown fox')
- // declarative, no state mutation, separation of side effects
- // both my examples do the same thing
- const latinize = phrase => (
- phrase.split(' ').reduce((newPhrase, word) => {
- const [ first, ...rest ] = word;
- return `${newPhrase} ${rest.join('')}${first}ay`
- }, '').trim()
- )
- console.log(latinize('quick brown fox'))
- const latinize = phrase => (
- phrase.split(' ').reduce((newPhrase, word, i) => {
- const [ first, ...rest ] = word
- return i === 0
- ? `${rest.join('')}${first}ay`
- : `${newPhrase} ${rest.join('')}${first}ay`
- }, '')
- )
- console.log(latinize('quick brown fox'))
- // another version of your solution that incorporates some of what I did
- // that might be more understandable to you
- (function latinize(phrase) {
- const latin = "ay ";
- const newPhrase = phrase.split(" ").reduce((accumulator, currentValue) => {
- const firstLetter = currentValue.substring(0, 1);
- const rest = currentValue.substring(1);
- return accumulator + rest + firstLetter + latin;
- }, "");
- console.log(newPhrase);
- })("quick brown fox");
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement