Exercice to apply conditionnel statement

Hello !
I had created a conditional statement to put the lesson into practice.
In the result I would have liked the ‘else’ output to return ‘date + birthday’.
But, I have not learned yet.
I tried to add get date = new Date () in console.log but it does not work!
Thank you for your help, it’s just for learning !:slightly_smiling_face:

let birthdayCake = ‘birthday cake’;

if (birthdayCake === ‘In January,It is my birthday !’) {
console.log(‘Strawberry pie with whipped cream !’);
} else if (birthdayCake === ‘In april, it’s my brother’s birthday.’) {
console.log(‘Raspberry cake with cream !’);
} else if (birthdayCake === ‘In March, It’s birthday of my grandson !’) {
console.log(‘Big black Forest with full of cream !’);
} else if (birthdayCake === ‘In May, It’s my eldest son’s birthday !’) {
console.log(‘A black Forest with full of cream !’);
} else if (birthdayCake === ‘In June, it’s mom and dad’s birthday.’) {
console.log(‘A big rhum baba and apricot pie can be ?’);
} else if (birthdayCake === ‘In July, It’s my little girl’s birthday !’) {
console.log(‘An amazing chocolate cake without cow’s milk !’);
} else if (birthdayCake === ‘In August, It’s the birthday of my youngest son !’) {
console.log(‘A Saint honored cake !’);
} else {
console.log(‘It’s not someone’s birthday that I know !’);
};
// It’s not someone’s birthday that I know !

Hey @domi_martin_1,

I am not sure what it is you want to achieve. So it is kinda hard for me to attempt to help you :wink: .

1 Like

I am not a pro or anything, instead a novice like you. Are you trying to create a program that offers one of those responses based upon the date?

I believe there is a built in function you can use to get the date in numbers, which is

getDate()

However the problem here is you created a variable named birthdayCake and assigned ‘birthday cake’ string to it. What your function thus far attempts to do is it checks if the birthdayCake variable’s string is equal to one of those stuff on your conditions.

Evidentally your birthdayCake’s string is ‘birthday cake’ so none of the conditions will be met and your program will execute the else statement in the end.

So maybe for else you could write…
const date = getDate.
So then you can invoke the console.log like;
console.log(‘Today is ’ + date + ’ and it is not anybody’s birthday as far as I am aware!’)

Just my five cents, hope it helps!

2 Likes

It seems she is only interested in the month. Best then extract the month from the current date and use that as input for the if…else conditions or use a switch.

var d = new Date();
var n = d.getMonth();

Extracts the month as an index of array of months. So if it returns 7 it is the 8th month, August.

Hello,
Thanks you to help me ! :slight_smile:
In fact, I would like to print that it is a person’s birthday based on the month of the year.
To do this, in the last line, you should include the date:
For example, we are in August: the output shows: it’s not the birthday of anyone I know, but it’s not the case! :slight_smile:
I do not know how to include the date with the last one. :slight_smile:

1 Like

Hey @domi_martin_1,

Wouldn’t you want a function that upon running tells the month and if it is someones birthday (as @meralbulak8877024881 mentioned) ? Otherwise I don’t know why you would like to print the date only in the “else” condition.

1 Like

Yes, it’s that I want and if it’s not the birthday of anyone I know, that’s saying it as well.
For this, I have to add the date in the code !

1 Like

Ok let me write some code :wink:

1 Like

Hello,
Thanks for your help.
I am a student too!
Yes, I wanted to do an exercise that gives as a result depending on the month and so 'else … if I gave.
But what if I only enter the date? Will it go for the birthday concerned?
Maybe it’s too advanced for my current level?
Good day to you :clin d’œil:

Hey I wrote two functions, one that looks at the current date and tells you cake to make.

And the second that allow you to type a month and it will tell what cake to make.

I could have used switch statements instead but I am not sure if you have learned about those yet.

const whatCakeThisMonth = () => {

  var date = new Date(); // Gets the date
  var month = date.getMonth(); // Extracts the month as index of an array of months
  
  if (month === 0 ) {
    console.log('It is January, it is my birthday! We will have Strawberry pie with whipped cream!');
  } else if (month === 2) {
    console.log('It is March, It\'s birthday of my grandson! We will make a Big black Forest with full of cream!');
  } else if (month === 3) {
    console.log('It is April, it\'s my brother\'s birthday. We will have Raspberry cake with cream!');
  } else if (month === 4) {
    console.log('It is May, It\'s my eldest son\'s birthday! A black Forest with full of cream!');
  } else if (month === 5) {
    console.log('It is June, it\'s mom and dad\'s birthday. A big rhum baba and apricot pie can be?');
  } else if (month === 6) {
    console.log('It is July, It\'s my little girl\'s birthday! An amazing chocolate cake without cow\'s milk for her!');
  } else if (month === 7 ) {
    console.log('It is August, It\'s the birthday of my youngest son! He loves a Saint honored cake!');
  } else {
    console.log('It is ' + month + ' It\'s not someone\'s birthday that I know !'); // Logs the current month which has no birthday
  };
}

const whatCakeIn = (monthInput) => {

  var monthInput = monthInput.toLowerCase(); // Writes everything in lower case just to be sure.

  if (monthInput === 'january' ) {
    console.log('In January, it is my birthday! We will have Strawberry pie with whipped cream!');
  } else if (monthInput === 'march') {
    console.log('In March, It\'s birthday of my grandson! We will make a Big black Forest with full of cream!');
  } else if (monthInput === 'april') {
    console.log('In april, it\'s my brother\'s birthday. We will have Raspberry cake with cream!');
  } else if (monthInput === 'may') {
    console.log('In May, It\'s my eldest son\'s birthday! A black Forest with full of cream!');
  } else if (monthInput === 'june') {
    console.log('In June, it\'s mom and dad\'s birthday. A big rhum baba and apricot pie can be?');
  } else if (monthInput === 'july') {
    console.log('In July, It\'s my little girl\'s birthday! An amazing chocolate cake without cow\'s milk for her!');
  } else if (monthInput === 'august' ) {
    console.log('In August, It\'s the birthday of my youngest son! He loves a Saint honored cake!');
  } else {
    console.log('In ' + monthInput + ' It\'s not someone\'s birthday that I know ! No need to make a cake.');
  };
};

whatCakeThisMonth();
whatCakeIn('September');
3 Likes

I wrote this for the last ‘else’ :
//

let birthdayCake = 'birthday cake';
const date = 'getdate';
if (birthdayCake === 'In January,It is my birthday !') {
  console.log('Strawberry pie with whipped cream !');
} else if (birthdayCake === 'In april, it\'s my brother\'s birthday.') {
  console.log('Raspberry cake with cream !');
} else if (birthdayCake === 'In March, It\'s birthday of my grandson !') {
  console.log('Big black Forest with full of cream !');
} else if (birthdayCake === 'In May, It\'s my eldest son\'s birthday !') {
	console.log('A black Forest with full of cream !');
} else if (birthdayCake === 'In June, it\'s mom and dad\'s birthday.') {
	console.log('A big rhum baba and apricot pie can be ?');
} else if (birthdayCake === 'In July, It\'s my little girl\'s birthday !') {
	console.log('An amazing chocolate cake without cow\'s milk !');
} **else if (birthdayCake === 'In August, It\'s the birthday of my youngest son !') {**
**  console.log('A Saint honored cake !');**
**} else {
	console.log('Today is' + date || 'and it is not anybody’s birthday as far as I am aware!');
};

//output : Today isgetdate

I think you did not learn the functions yet, which is why you probably did not understand what they suggested above. Those functions are awesome by the way, however to go over what you just wrote, problem is this:

const date = 'getdate'


// and later on you wrote at the final else condition

else {
	console.log('Today is' + date || 'and it is not anybody’s birthday as far as I am aware!');
};

For starters you did not make a mistake, it was my mistake :slight_smile:
Problem is getDate is the name of the function.
So when you say const date = ‘getdate’ |

What it does is;
It says date variable’s content is ‘getdate’ string. You need to remove these signs from around it ’ '.

Now if you typed
const date = getDate

It takes the getDate function and stores it into a variable named date.
I’m trying to explain this as clearly as possible, the mistake I made here is that you are merely assigning a shortcut to another function. You are not running the function, thus you are not acquiring a data to return back to you.

So my suggestion to you would be to:
const date = getDate()
1- Don’t forget it is getDate not getdate, JavaScript is case sensitive.
2- Add the paranthesis afterwards so that it runs the function.
3- Remove these symbols ’ ’ | Otherwise the program will think you are simply writing text.


However if you go over what @janneslohmeijer said, it’d be a lot better. Their functions do what you actually want to achieve in a much cleaner version. Although it might be a bit complex for you because you do not know what functions are or why he checks them with numbers like 0 2 3 4 5 6 7. (When you get to lists you’ll find out about elements and their order within said lists. First element always starts with 0, the next 1 and so on so on.

Basically what this means is:

Months = [‘January’, ‘February’, ‘March’, ‘April’, ‘May’, ‘June’, ‘July’, ‘August’, ‘September’, ‘October’, ‘November’, ‘December’]

Basically January is 0, February is 1, March is 2 so on so on.
Don’t worry if it feels too overwhelming, you’ll learn all of these in the next few chapters!

3 Likes

Thank you very much for all these details @meralbulak8877024881 !
I will read the two corrections of @janneslohmeijer
In fact, I had formulated the first line incorrectly so I could not answer the last ‘else’.
I’m happy ! I have learn how I can do !
Thank you for your lights :+1::+1:

1 Like

:rofl:
So happy !
Thank you very much for your help :+1::+1::+1:
I understood all the code you wrote.
I had poorly formatted the start so the end could not match the result I was looking for.
I keep your corrections in my space very carefully in order to acquire the ‘mechanism’ of the mind to have.
I wish you a good day !
Soon I hope I can help others too :upside_down_face:

1 Like