Hello Glitch Users!
I am in need of some homework help… Currently am enrolled in a Javascript class and the teacher takes forever sometimes to reply (which I understand teachers are busy and underpaid) I need help with the following… Any and all help would be greatly appreciated as I am super confused and can not seem to figure any of this out.
uses prompt() to ask the user for a month number. It should then use two separate decision structures (one with switch, one with if/if-else) to output the corresponding month name and the number of days in that month. For example, entering “5” should identify the month as May and indicate that it has 31 days.
imo try breaking down the task into smaller parts and seeing if you know how to do any of those parts. what similar things have you worked on earlier in the class?
Thank you for replying! We have been working on conditionals as of this week. Here is some of the code that I have so far, in theory is this kind of in the ballpark?
html>
<body>
<script>
var month = prompt("Please provide a month number");
if (month = 1) {
document.write ("The current month is January")
}
</script>
</body>
Hi @mikeisbomb92! @wh0’s answer basically says everything, but here’s some exemplification.
1. If/else
You could do something like this:
month = parseInt(month) // this will transform the number of the month that the user sent to a number. For example, converts "1" to 1
if (month == 1) {
} else if (month == 2) {
} else if (month == 3) {
// and continue like this until
} else if (month == 12) {
} else {
// INVALID MONTH!
}
2. Switch statement
Are you sure? This will probably be a spoiler.
I'm sure, captain!
Try this:
// no need to parseint here, adjust manually if you want to
switch (month) {
case '1':
// logic for month 1
break;
case '2':
// logic for month 2
case '3':
// logic
break;
case X:
// and continue like this until...
break;
default:
// Month does not exist!
}
@wh0@tiagorangel2011 Thanks for the help! I have to use either an if or an if-else statement followed by a switch conditional as well. Any and all help would be appreciated Would I somehow include the switch conditional with the else-if? Or do I deal with the month number then handle how many days are in each month?
So code speaking wise do I have somewhat correct code? Here is a little sample so far. Im just curious how I would go about this
<body>
<script>
var month = prompt("Please provide a month number");
if (month == 1) {
document.write ("The current month is January")
}
else if (month == 2) {
document.write ("The current month is February")
}
else if (month == 3) {
document.write ("The current month is March")
}
else if (month == 4) {
document.write ("The current month is April")
}
else if (month == 5) {
document.write ("The current month is May")
}
else if (month == 6) {
document.write ("The current month is June")
}
else if (month == 7) {
document.write ("The current month is July")
}
else if (month == 8) {
documnt.write ("The current month is August")
}
else if (month == 9) {
documnt.write ("The current month is September")
}
else if (month == 10) {
document.write ("The current month is October")
}
else if (month == 11) {
document.write ("The current month is November")
}
else if (month == 12) {
documnet.write ("The current month is December")
}
</script>
</body>
After this is completed, where exactly would the switch come in? I am required to use both.
Hi! You could Change the first code to instead of alerting, adding a variable with the month name. Then, do the same with the switch and call everything on the final alert.
How would I start this? Sorry for so many questions! We are learning about these statements this week so it is not coming very easy to me but I am determined to figure this out. So you are saying instead of using “var month = prompt(“Please provide a month number”)” add a variable with the month name, so it would look like “var monthName?” Then call for the monthNumber? Thanks again for all the help and tips!
var monthNumber = prompt("Please provide a month number");
monthNumber = parseInt(monthNumber) // this will transform the month number into a number
/* we will change this variables later on the code */
var monthName;
var monthDays;
/* This will set up the month name */
if (monthNumber == 1) {
monthName = "January";
} else if (monthNumber == 2) {
monthName = "February";
} else if (monthNumber == 3) {
monthName = "March";
} else if (monthNumber == 4) {
monthName = "April";
} else if (monthNumber == 5) {
monthName = "May";
} else if (monthNumber == 6) {
monthName = "June";
} else if (monthNumber == 7) {
monthName = "July";
} else if (monthNumber == 8) {
documnt.write("The current month is August";
} else if (monthNumber == 9) {
documnt.write("The current month is September";
} else if (monthNumber == 10) {
monthName = "October";
} else if (monthNumber == 11) {
monthName = "November";
} else if (monthNumber == 12) {
monthName = "December";
}
/* Now we need to use switch to the month days part. */
/* I'm going to leave this as a to-do for you */
switch (month) {
case 1:
monthDays = 31;
break;
case 2:
monthDays = 28;
break;
case 3:
monthDays = 31;
break;
// and continue like this
}
/* Now let's output the information! */
alert(`Month ${monthName} has ${monthDays} days.`)
Sorry, I rushing to get another assignment done, I am swamped, but your code looks good, Remember to close all statements with a ;
Here’s how mine came out:
// Set the Birth Month of the user.
let days;
let Month = prompt(
"What Month were you born in? Please enter the number of the correlating Month."
);
// checking to see what Month it is.
if ((Month = "01")) {
document.write(
"You were Born in: " + Month + " Which has " + days + " days in it."
);
} else if ((Month = "02")) {
document.write(
"You were Born in " + Month + " which has" + days + " days in it."
);
} else if (Month == "03") {
document.write(
"You were Born in " + Month + " which has" + days + " days in it."
);
} else if (Month == "04") {
document.write(
"You were Born in " + Month + " which has" + days + " days in it."
);
} else if (Month == "05") {
document.write(
"You were Born in " + Month + " which has" + days + " days in it."
);
} else if (Month == "06") {
document.write(
"You were Born in " + Month + " which has" + days + " days in it."
);
} else if (Month == "07") {
document.write(
"You were Born in " + Month + " which has" + days + " days in it."
);
} else if (Month == "08") {
document.write(
"You were Born in " + Month + " which has" + days + " days in it."
);
} else if (Month == "09") {
document.write(
")You were Born in " + Month + " which has" + days + " days in it."
);
} else if (Month == "10") {
document.write(
"You were Born in " + Month + " which has" + days + " days in it."
);
} else if (Month == "11") {
document.write(
"You were Born in " + Month + " which has" + days + " days in it."
);
} else if (Month == "12") {
document.write(
"You were Born in " + Month + " which has" + days + " days in it."
);
}