Last active
January 24, 2021 19:57
-
-
Save rjensen96/1612b3e98a64203a18223845cda532ad to your computer and use it in GitHub Desktop.
@adamdenes mixed messages
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
const messages = { | |
starter: ['knock'], | |
subject: ['goliath', 'broccoli', 'wooden shoe', 'boo', 'cows go', 'harry'], | |
ending: [ | |
'down, you look-eth tired', | |
'doesn’t have a last name, silly', | |
'shoe like to hear another joke', | |
'why are you crying', | |
'no silly, cows go MOO', | |
'up and answer the door' | |
], | |
punctuation: ['.', '!', '?'] | |
}; | |
const getRandomItem = arr => { | |
return arr[Math.floor(Math.random() * arr.length)]; | |
} | |
const toCapital = string => { | |
const firstLetter = string.charAt(0).toUpperCase(); | |
const restOfString = string.slice(1); | |
return firstLetter + restOfString; | |
} | |
const generateMessage = obj => { | |
let msg = ''; | |
// return values object mirrors messages object | |
const values = { | |
starter: null, | |
subject: null, | |
ending: null, | |
punctuation: null, | |
} | |
// mirrored objects simplify this loop | |
for(let key in obj) { | |
values[key] = getRandomItem(obj[key]); | |
} | |
// using "values.item" made msg long, so I split it up | |
msg = `${toCapital(values.starter)}-${values.starter}${values.punctuation}`; | |
msg += ` Who's there? ${toCapital(values.subject)}. `; | |
msg += `${toCapital(values.subject)} who${values.punctuation} ${toCapital(values.subject)} ${values.ending}${values.punctuation}`; | |
return msg; | |
}; | |
console.log(generateMessage(messages)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Really cool idea! Thank you for the suggestion @rjensen96! :)