d2jsp
Log InRegister
d2jsp Forums > Off-Topic > General Chat > Homework Help > Javascript Homework > Help; Easy 50fg
Add Reply New Topic New Poll
Member
Posts: 9,755
Joined: Sep 6 2009
Gold: 197.00
Feb 14 2017 09:28pm
Create a program which displays the current season for each month of the year: Winter, Spring, Summer, or
Fall. Use the following information to write the program:

• Use a counter variable named month. The month variable will have a range of 1-12. The 1-12 range
represents the months of the year. Begin with 1 for January, end with 12 for December. Stop
processing when the month value is 12.

• Increment the month variable in one month increments (1, 2, 3, etc.) only.

• The season to display is determined by the following rules:
o "Winter" if the month variable's value is 12, 1, or 2.
o "Spring" if the month variable's value is in the range of 3-5.
o "Summer" if the month variable's value is in the range of 6-8
o "Fall" if the month variable's value is in the range of 9-11.

• The correct season should be displayed for each month based on the preceding rules.
o The same greeting will be displayed repeatedly while the condition is true. i.e. while the current
month value is within a particular range of months (see the output example below).

• Do not display the month number or month name, only display the season the month falls in.


You will need to use the following language elements:
1. A numeric variable called month which should be initialized to a starting value of 1 (1 represents
January).
2. A while loop to process the months as long as it is within the range January (1) through December (12).
a. You will need to increment the month variable by one for each iteration of the while loop. The month
variable represents the current month of the year.
3. An if/else if statement to determine the season to display based on the current value of the month variable.
4. Use the document.write function for output. Remember, each season will be displayed multiple times for a
month range (Winter, Spring, Summer, Fall).

The output should be as follows:

Winter
Winter
Spring
Spring
Spring
Summer
Summer
Summer
Fall
Fall
Fall
Winter
Member
Posts: 9,755
Joined: Sep 6 2009
Gold: 197.00
Feb 15 2017 08:07pm
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<!-- HTML5 JS Template - CMSY 172
Use this template to create new JS pages.
-->

<!-- your name - Lab #: JS Template - Creation Date -->

<title>Lab #2</title>

<c>schoi_lab2</c>
<c>This program displays the seasons of the month.</c>

</head>
<body>


<!-- Enter all JavaScript code within the HTML script element below. Generally, the script element should be the last
element before the closing body tag.
-->
<script>

"use strict";

var month = 1;

while(month<12){

if(month =1)
document.write("Winter.<br>");

}if(month=2){
document.write("Winter.<br>");

}if(month=3){
document.write("Spring.<br>");

}if(month=4){
document.write("Spring.<br>");

}if(month=5){
document.write("Spring.<br>");

}if(month=6){
document.write("Summer.<br>");

}if(month=7){
document.write("Summer.<br>");

}if(month=8){
document.write("Summer.<br>");

}if(month=9){
document.write("Fall.<br>");

}if(month=10){
document.write("Summer.<br>");

}if(month=11){
document.write("Summer.<br>");

}if(month=12){
document.write("Winter.<br>");

}//end of if - display greeting

month = month + 1;

}//End while loop

</script>

</body>
</html>


This is what I came up with but its not working. Can anyone help?

This post was edited by Panguin on Feb 15 2017 08:08pm
Member
Posts: 1,234
Joined: Dec 10 2010
Gold: 13,637.72
Feb 16 2017 12:14am
Quote (Panguin @ Feb 15 2017 10:07pm)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<!-- HTML5 JS Template - CMSY 172
Use this template to create new JS pages.
-->

<!-- your name - Lab #: JS Template - Creation Date -->

<title>Lab #2</title>

<c>schoi_lab2</c>
<c>This program displays the seasons of the month.</c>

</head>
<body>


<!-- Enter all JavaScript code within the HTML script element below. Generally, the script element should be the last
element before the closing body tag.
-->
<script>

"use strict";

var month = 1;

while(month<12){

if(month =1)
document.write("Winter.<br>");

}if(month=2){
document.write("Winter.<br>");

}if(month=3){
document.write("Spring.<br>");

}if(month=4){
document.write("Spring.<br>");

}if(month=5){
document.write("Spring.<br>");

}if(month=6){
document.write("Summer.<br>");

}if(month=7){
document.write("Summer.<br>");

}if(month=8){
document.write("Summer.<br>");

}if(month=9){
document.write("Fall.<br>");

}if(month=10){
document.write("Summer.<br>");

}if(month=11){
document.write("Summer.<br>");

}if(month=12){
document.write("Winter.<br>");

}//end of if - display greeting

month = month + 1;

}//End while loop

</script>

</body>
</html>


This is what I came up with but its not working. Can anyone help?


You're missing an opening brace here:

Code
if(month =1){
document.write("Winter.<br>");


That should work.

This post was edited by Comc on Feb 16 2017 12:14am
Member
Posts: 1,234
Joined: Dec 10 2010
Gold: 13,637.72
Feb 16 2017 12:19am
Also, you should be using "==" for equality testing. You're just reassigning the variable month in every if statement, which happens to work since you're just printing all the seasons in sequence anyway. Note that the while loop only ever executes once since month is already 12 after the first iteration.
Member
Posts: 9,755
Joined: Sep 6 2009
Gold: 197.00
Feb 16 2017 11:13am
Quote (Comc @ Feb 16 2017 02:19am)
Also, you should be using "==" for equality testing. You're just reassigning the variable month in every if statement, which happens to work since you're just printing all the seasons in sequence anyway. Note that the while loop only ever executes once since month is already 12 after the first iteration.


<br/><script>


var month = 1;

while(month<13){

if(month < 3){
document.write("Winter<br>");

}else if(month < 6){
document.write("Spring<br>");

}else if(month < 9){
document.write("Summer<br>");

}else if(month <12){
document.write("Fall<br>");

}else {
document.write("Winter<br>");

}//end of if - display greeting

month = month + 1;

}//End while loop

</script>

I ended up figuring it out, but I appreciate the reply. Sent you a small token of my appreciation
Member
Posts: 16,138
Joined: Feb 6 2014
Gold: 2,985.99
Warn: 40%
Feb 18 2017 11:50am
yes your braces in if statement are all wrong that is why
Go Back To Homework Help Topic List
Add Reply New Topic New Poll