Can anyone help me get this functional? Mixed messages

Im trying to keep it very simple, not sure if this is something that can work…
const mixedMessages = () => {

const wordsArray1 = ['want to', 'need to', 'have to', 'get to'];

const wordsArray2 = ['run','jump','laugh','dance'];

const wordsArray3 = ['in the sunshine','in the moonlight','all night long','every day'];

let word1 = wordsArray1[Math.floor(Math.random() * 3)];

let word2 = wordsArray2[Math.floor(Math.random() * 3)];

let word3 = wordsArray3[Math.floor(Math.random() * 3)];

return `I + ${word1} + ${word2} + ${word3}!`

}

console.log(mixedMessages());

Figured it out!!!

const mixedMessages = () => {

const wordsArray1 = ['want to', 'need to', 'have to', 'get to'];

const wordsArray2 = ['run','jump','laugh','dance'];

const wordsArray3 = ['in the sunshine','in the moonlight','all night long','every day'];

let word1 = wordsArray1[Math.floor(Math.random() * 3)];

let word2 = wordsArray2[Math.floor(Math.random() * 3)];

let word3 = wordsArray3[Math.floor(Math.random() * 3)];

return `I  ${word1}  ${word2}  ${word3}!`

}

console.log(mixedMessages());

1 Like
const mixedMessages = function () {
  const r = arr => arr[Math.floor(Math.random() * 3)];
  u = ['run', 'jump', 'laugh', 'dance'];
  v = ['run', 'jump', 'laugh', 'dance'];
  w = ['in the sunshine', 'in the moonlight', 'all night long', 'every day'];
  return `I ${r(u)} ${r(v)} ${r(w)}!`
}

Just an example of how to avoid repetition.

You will never get last elements from your arrays with this code. You should multiply to 4 or better use array.length property to calculate proper value.

Good eye! Was waiting for that to be pointed out.

Thank you for that i noticed it in testing it out

I agree on getting used to using the array.length property as the default value so you never have to worry about the specific number of items in the array.