Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
@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

This comment has been minimized.

Copy link

@adamdenes adamdenes commented Jan 24, 2021

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