Skip to content

Instantly share code, notes, and snippets.

@rjensen96
Last active January 24, 2021 19:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rjensen96/1612b3e98a64203a18223845cda532ad to your computer and use it in GitHub Desktop.
Save rjensen96/1612b3e98a64203a18223845cda532ad to your computer and use it in GitHub Desktop.
@adamdenes mixed messages
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));
@adamdenes
Copy link

Really cool idea! Thank you for the suggestion @rjensen96! :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment