Mixed Messages Project

Hey Guys,

Just done the “Mixed Messages” Portfolio Program. a bit challenging but fun,

Appreciate your feedback on my code and how i could improve.


Hello, and welcome to the forums!

First, good job completing the project! A knock-knock joke generator is a nice idea

One thing to consider is this loop:

  for (let i = 0; i < knockJokes.names.length; i++) {
    name = ranName;
    console.log(`${name} Who?`);

There’s no need for the loop. You only run this block of code once because of your break and you don’t use the value of i either. Changing it to this would be the same output:

    name = ranName;
    console.log(`${name} Who?`);

Your other loop is iterating through another array to find the matching name, which is working fine and isn’t necessarily a problem because it’s such a small list. Had this been a program with many more joke possibilities, storing the name and the corresponding joke together would be better. Alternatively, you could store the random number you generate and use the same index to access both the jokes and names since you currently have their corresponding values in the same place in each array, but this could be error-prone when adding more jokes in the future.

Well done! I really enjoy the concept and the jokes were cute

1 Like

Hi Meezdev,
I think you could improve line number 14 and 16, instead of two separate statement you could
use a function and reuse it and pass it different arguments each time.
like so:

const randomNumber = (object) => {
  return object.name[Math.floor(Math.random(object.name.length) * 5)];
1 Like

Thanks for great words and support.

when you said

Had this been a program with many more joke possibilities, storing the name and the corresponding joke together would be better

do you mean, an object that has an array for each joke? where each joke has keys and values?

Here are some examples with some notes:

// Your existing data structure
// An object with two separate arrays, but the values of each array depend on each other
// You're currently using a for-loop to search through `jokes` to find the matching name
// Instead, you could generate the random index once and use the same index on both arrays
// to avoid the loop because both arrays have the values in the same spots. But if you
// aren't careful with future additions or removals, it could be prone to errors if the
// array spots don't match up
const knockJokes = {
  names: ["Goliath", "Broccoli", "Wooden shoe", "Amish", "Boo"],
  jokes: [
    "Goliath down, you look-eth tired!",
    "Broccoli doesn’t have a last name, silly.",
    "Wooden shoe like to hear another",
    "Really Amish?  You don’t look like a shoe!",
    "Boo! Why are you crying?",

// An array of objects
// You could generate a random index to retrieve an object
// The object would have a 'name' and 'joke' property that match without a loop
const knockJokes2 = [
    name: 'Name 1',
    joke: 'Name 1 funny'
    name: 'Name 2',
    joke: 'Name 2 funny'
    name: 'Name 1',
    joke: 'Name 3 funny'

// Just an object
// The keys are the names, the values are the jokes
// You could get an array of keys with Object.keys(), then pick a random one
// in order to access the joke.
// This would limit some possibilities though because the keys have to be unique,
// which means you couldn't have the same name twice, even if it's a different joke.
const knockJokes3 = {
  'Name 1': 'Name 1 funny',
  'Name 2': 'Name 2 funny',
  'Name 3': 'Name 3 funny'

These are just some possibilities that would eliminate the loop that does a text search to find the matching joke. At some point, your list could be so big that it would be best in a database anyway.

Like I said, you don’t have to worry about it with such a small list, but it’s something to keep in mind as you continue your journey.

1 Like

Your’e great. Thank you!