Mixed Messages - Code Not Functioning Properly, Please Help!

Hi!

I’m having issues with the Mixed Messages project in the full-stack developer course (https://www.codecademy.com/paths/full-stack-engineer-career-path/tracks/fscp-javascript-syntax-portfolio-project/modules/fscp-mixed-messages/kanban_projects/mixed-messages).

For some reason my code isn’t printing a different joke when one of the initial randomly chosen indexes is the same as one of the other indexes. It just prints the joke using the initially chosen index.

Could someone help me fix my code, please? It’s here:

const jokes = ['My wife told me to stop impersonating a flamingo. I had to put my foot down.', 'I went to buy some camo pants but couldn’t find any.', 'I failed math so many times at school, I can’t even count.', 'I used to have a handle on life, but then it broke.', 'I was wondering why the frisbee kept getting bigger and bigger, but then it hit me.', 'I heard there were a bunch of break-ins over at the car park. That is wrong on so many levels.', 'When life gives you melons, you might be dyslexic.', 'Don’t you hate it when someone answers their own questions? I do.', 'I told him to be himself; that was pretty mean, I guess.', 'Never trust atoms; they make up everything.'];

//Function to randomly select an index in jokes array
const generateTarget = () => {
  return Math.floor(Math.random() * 10);
};

//Randomly select index for each joke
let joke1 = generateTarget();
console.log(joke1)
let joke2 = generateTarget();
console.log(joke2)
let joke3 = generateTarget();
console.log(joke3)

//Function to choose a new index if two of the same index are chosen so that three different jokes are selected later
const ifJokesAreSame = (joke1, joke2, joke3) => {
if (joke1 === joke2) {
  do {
    joke1 = generateTarget(); //This is to select a new index       
  }
  while (joke1 === joke2 || joke1 === joke3); //This is to re-run the function to select a new index until a unique one is chosen
}
if (joke1 === joke3) {
  do {
    joke1 = generateTarget();       
  }
  while (joke1 === joke3 || joke1 === joke2);
}
if (joke2 === joke3) {
  do {
    joke2 = generateTarget();       
  }
  while (joke2 === joke3 || joke2 === joke1);
}
}

//Function to select and print jokes
const selectJoke = () => {
    console.log(jokes[joke1]);
    console.log(jokes[joke2]);
    console.log(jokes[joke3]);
}

//Call function to select jokes
selectJoke()

Hi @board9685990051
I cannot see that you are calling your function ifJokesAreSame anywhere…

Sorry, I removed it when editing my code then forgot to add it later!

I just checked and I’m still getting the same error even after calling the ifJokesAreSame function.

Here is the updated code:

const jokes = ['My wife told me to stop impersonating a flamingo. I had to put my foot down.', 'I went to buy some camo pants but couldn’t find any.', 'I failed math so many times at school, I can’t even count.', 'I used to have a handle on life, but then it broke.', 'I was wondering why the frisbee kept getting bigger and bigger, but then it hit me.', 'I heard there were a bunch of break-ins over at the car park. That is wrong on so many levels.', 'When life gives you melons, you might be dyslexic.', 'Don’t you hate it when someone answers their own questions? I do.', 'I told him to be himself; that was pretty mean, I guess.', 'Never trust atoms; they make up everything.'];

//Function to randomly select an index in jokes array
const generateTarget = () => {
  return Math.floor(Math.random() * 10);
};

//Randomly select index for each joke
let joke1 = generateTarget();
console.log(joke1) //This is to check my output
let joke2 = generateTarget();
console.log(joke2) //This is to check my output
let joke3 = generateTarget();
console.log(joke3) //This is to check my output

//Function to choose a new index if two of the same index are chosen so that three different jokes are selected later
const ifJokesAreSame = (joke1, joke2, joke3) => {
if (joke1 === joke2) {
  do {
    joke1 = generateTarget(); //This is to select a new index       
  }
  while (joke1 === joke2 || joke1 === joke3); //This is to re-run the function to select a new index until a unique one is chosen
}
if (joke1 === joke3) {
  do {
    joke1 = generateTarget();       
  }
  while (joke1 === joke3 || joke1 === joke2);
}
if (joke2 === joke3) {
  do {
    joke2 = generateTarget();       
  }
  while (joke2 === joke3 || joke2 === joke1);
}
}

//Calling function to make sure indexes are all different
ifJokesAreSame(joke1, joke2, joke3);

//Function to select and print jokes
const selectJoke = () => {
    console.log(jokes[joke1]);
    console.log(jokes[joke2]);
    console.log(jokes[joke3]);
}

//Calling function to select jokes
selectJoke()

It’s a matter of scope. The reassigned variables are only locally available. I would consider using one array for all indexes.

I think another thing to think of is how you approach the problem. My inclination would be (if my intention is to not have duplicate jokes) to stop the duplication from happening rather than check after-the-fact.

I did a version of this below:

This is also scaleable and will operate off of as many jokes as you want
let jokes = ['My wife told me to stop impersonating a flamingo. I had to put my foot down.', 'I went to buy some camo pants but couldn’t find any.', 'I failed math so many times at school, I can’t even count.', 'I used to have a handle on life, but then it broke.', 'I was wondering why the frisbee kept getting bigger and bigger, but then it hit me.', 'I heard there were a bunch of break-ins over at the car park. That is wrong on so many levels.', 'When life gives you melons, you might be dyslexic.', 'Don’t you hate it when someone answers their own questions? I do.', 'I told him to be himself; that was pretty mean, I guess.', 'Never trust atoms; they make up everything.'];


//Function to select and print jokes
const selectJoke = (num) => {
 if (num > jokes.length){ //checking to see if invalid input, if so, set to max allowed.
   num = jokes.length;
 }
 if(num <= 0){
   return console.log(`Please enter a number greater than 0.`)
 }
  const jokesCopy = jokes.slice(); //copy array within scope, which allows the original array to be referenced in original form. This makes it non-destructive.
  
  for(let i = 0; i < num; i++){ //loops through the amount of jokes I selected
    const jokeIndex = Math.floor(Math.random() * jokesCopy.length); //index based on array length
    console.log(jokesCopy[jokeIndex]); //prints my joke
    jokesCopy.splice(jokeIndex, 1); // removes that joke from the list of available jokes
  }
  
}

//Calling function to select jokes and how many
selectJoke(6)

There is no wrong or right way necessarily, but this was just my thoughts as I was going through this problem.

Ah, I see it now! I have solved the problem! Thank you very much! :grinning:

1 Like

Wow! That is so much more useful than my code! Thank you so much, that way of thinking is really helpful! :grinning: