Making a code that will pick a movie based on mood

Hello :slight_smile:

I have completed the first three chapters on Codecademy. I’m trying to bring what I’ve learnt together by making a code that would suggest a film choice to my boyfriend depending on his mood.

Using if/else, from Chapter 2, I was able to make a code that ran just once, but if I tried to change the mood variable, after the if/else block, nothing happened, so I suppose that means I would have to paste in the if/else block each time the mood variable was changed.

I thought creating a function might help, so I could call upon it each time the mood variable changes. However, I found Chapter 3 a bit harder to understand than the first two, so I’m not sure if I’m using functions correctly here.

At the moment, I can’t get anything of my if/else options to appear in the console. I would like to create a piece of code that suggests a film depending on the mood variable and if I change the mood variable, I can just call the function, without having to write the if/else code again.

Any help would be much appreciated!

let mood;
function movieChoice(mood){
if (mood === ‘sad’) {
console.log(“Play ‘Lord of the Rings’”)
} else if (mood === ‘sick’){
console.log(“Play ‘Harry Potter’”);
} else if (mood === ‘happy’){
console.log(“Play ‘A Knight’s Tale’”);
} else {
console.log(“Read a book.”);
}
};
return mood;
mood = ‘sad’;
console.log(movieChoice());

A few things:
The return statement or statements (one for each mood) should be inside the function so that the function is returning the string. It is not doing anything productive outside of the function.

You are doubling up on console.log. First you are logging the return of the movieChoice() function and then you have console.log inside of the function. It is only needed in one place and I would say usually better to keep it outside of the function and just have the function return the string.

Currently you are declaring a global variable mood and referencing it inside the function. Therefore it is not needed as a parameter. Keeping it both as a function parameter and a global variable (with the same name) creates confusion in the code. I would recommend removing the global variable mood and calling movieChoice with a parameter and then using console.log on the return value from that function but this can be done a few different ways…

Hey byte2112864556

If you indent your code you’ll see that you got a major problem with it, you have the return statement outside the function:

let mood;
function movieChoice(mood){
  if (mood === ‘sad’) {
    console.log(“Play ‘Lord of the Rings’”)
  } else if (mood === ‘sick’){
    console.log(“Play ‘Harry Potter’”);
  } else if (mood === ‘happy’){
    console.log(“Play ‘A Knight’s Tale’”);
  } else {
    console.log(“Read a book.”);
  }
};
return mood;
mood = ‘sad’;
console.log(movieChoice());

If you actually need this function to return anything you must always place the return keyword inside it.

However in your case, I think you don’t even need the return statement, because you’re returning mood which you’re not manipulating inside your function and it’s a global variable, so it’s value doesn’t change and it’s always going to be accessible from outside the function.

Also, the quotes used in this code are not correct, but it seems is a problem related to how you pasted the code in here, however be sure to use either single quotes ' or double quotes only "

Your code corrected would look like this:

function movieChoice(mood){
  if (mood === "sad") {
    console.log("Play Lord of the Rings")
  } else if (mood === "sick"){
    console.log("Play Harry Potter");
  } else if (mood === "happy"){
    console.log("Play A Knight's Tale");
  } else {
    console.log("Read a book.");
  }
};
const mood = "sad";
console.log(movieChoice(mood));

//output is:
//Play Lord of the Rings

Hope that this helps :slight_smile:

Thank you. That was a super helpful explanation. I found the concept of “return” to be quite confusing - still getting to grips with it.

May I ask why you chose const? This means the variable cannot be changed, so wouldn’t var make more sense in the context of what’s trying to be achieved with the code?

Also, the output is currently printing the quote, such as “Play ‘Lord of the Rings’”, but also on the next line it’s printing undefined. What’s causing this?

Thank you

You’re welcome

May I ask why you chose const?

You may

so wouldn’t var make more sense

var or let (here a post about the difference) would make sense if you expect to re-assign or re-declare the variable, in your case mood. (Here a thread on SO about const)

So if you need to re-declare mood you cannot use const. In the code I posted there was not this need and const was ok.

in the context of what’s trying to be achieved with the code?

Things in code can be achieved in many different ways. You could store information in an array or an object and retrieve this information later on for example, instead of hardwriting it into the function :wink:

What’s causing this?

Here’s your answer

:wink: