Question

Convert the following JavaScript code by replacing the switch statement with an equivalent (nested) if-statement :...

Convert the following JavaScript code by replacing the switch statement with an equivalent (nested) if-statement :
Month = today.getMonth();
switch (Month)
{
case 0:
case 1:
case 11:
season = "winter";
break;
case 2:
case 3:
case 4:
season = "spring";
break;
case 5:
case 6:
case 7:
season = "summer";
break;
default:
season = "fall";
}

Homework Answers

Answer #1

Description:

In the switch statement if 'Month' value is 0 or 'Month' value is 1 or 'Month' value is 11, the statement season =Winter; will be executed. (We can write it as if(Month==0||Month==1||Month==11) { season = "winter"; } ).

We can represent remaining two (cases 2,3,4 and cases 5,6,7) as above.

The last case (default case) is executed if all the above statements are not true.

Answer:

Month = today.getMonth();

if(Month==0||Month==1||Month==11)

{

season = "winter";

}

else if(Month==2||Month==3||Month==4)

{

season = "spring";

}

else if(Month==5||Month==6||Month==7)

{

season = "summer";

}

else

{

season = "fall";

}

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
19) What will be the output of the following JavaScript code? for(var x = 1; x...
19) What will be the output of the following JavaScript code? for(var x = 1; x < 5; x++) console.log(x); a) 11111 b) 12345 c) 1234 d) 5555 20) What will be the output of the following JavaScript code? var x = 0 do{ console.log(x) }while(x > 0) a) 0 b) null c) 1 d) No output quiz 3 4) Which of the following is an entry point of ASP.NET Core application? a) Main method of Program class b) Configure...
C++ Code! Represent the following matrix using a two-dimensional array and write a nested for loop...
C++ Code! Represent the following matrix using a two-dimensional array and write a nested for loop to initialize the elements (do not initialize the array in the declaration): 10  9   8 7   6   5 4 3   2
Java programming. 1) What, if anything, is wrong with the following Java code? void test(String x)...
Java programming. 1) What, if anything, is wrong with the following Java code? void test(String x) { switch (x) { case 1: break; case 2: break; case 0: break; default: break; case 4: break; } } Select one: a)Each case section must end with a break statement. b)The case label 0 must precede case label 1. c)There is nothing wrong with the Java code. d)The default label must be the last label in the switch statement. e)The variable x does...
Directions: Convert the following problems below to c++ equivalent code Problem 6: Get a random number...
Directions: Convert the following problems below to c++ equivalent code Problem 6: Get a random number from the user Remember the original number Add 3 to the number Double the number and then multiply by Knock off the last digit (remove the 1’s place) Finally remove the original number What is the result? Problem 7: 1: Have the person write down any three digits number with decreasing digits (432 or 875). 2: Reverse the number you wrote in #1. 3:...
7. (True/False): Nested procedure calls are not permitted by the Microsoft assembler unless the NESTED operator...
7. (True/False): Nested procedure calls are not permitted by the Microsoft assembler unless the NESTED operator is used in the procedure definition. 15. What will be the final value in EAX after these instructions execute? push 5 push 6 pop eax pop eax 3. Which instruction pops the stack into the EFLAGS register? 17. Which statement is true about what will happen when the example code runs? 1: main PROC 2: mov eax,30 3: push eax 4: push 40 5:...
4. The _____ case is performed by the switch statement if a match is not found...
4. The _____ case is performed by the switch statement if a match is not found default first second otherwise Answer: 5. When three relational expressions are joined by OR (||) operators, there must be at least _____ of the relational expressions that evaluate to true for the logical expression to evaluate to true. one two three none of the relational expressions evaluate to true Answer: 6. Which of the following items is an acceptable first character in a Java...
The ability to read through Java™ code and predict the results, given specific inputs, is an...
The ability to read through Java™ code and predict the results, given specific inputs, is an extremely useful skill. For this assignment, you will be analyzing the Java™ code in the linked zip file, and predicting the results given specific input. Carefully read through the code line by line, then answer the following questions in a Microsoft® Word document: What is the output of the program as it is written? What would the output of the program be if you...
Re-write following if-else-if statements as Switch statement. Your final code should result in the same output...
Re-write following if-else-if statements as Switch statement. Your final code should result in the same output as the original code below. if (selection == 10) System.out.println("You selected 10."); else if (selection == 20) System.out.println("You selected 20."); else if (selection == 30) System.out.println("You selected 30."); else if (selection == 40) System.out.println("You selected 40."); else System.out.println("Not good with numbers, eh?"); Second question Re-write following while loop into Java statements that use a Do-while loop. Your final code should result in the same...
Q1: Re-write following if-else-if statements as Switch statement. Your final code should result in the same...
Q1: Re-write following if-else-if statements as Switch statement. Your final code should result in the same output as the original code below. if (selection == 10) System.out.println("You selected 10."); else if (selection == 20) System.out.println("You selected 20."); else if (selection == 30) System.out.println("You selected 30."); else if (selection == 40) System.out.println("You selected 40."); else System.out.println("Not good with numbers, eh?"); Q2: Re-write following while loop into Java statements that use a Do-while loop. Your final code should result in the same...
1. Given the following multi-way if statement, provide a switch statement, using proper java syntax, that...
1. Given the following multi-way if statement, provide a switch statement, using proper java syntax, that will provide the same function. Char grade; String tstmsg; if (grade == ‘A’) {   tstmsg = “Excellent”; } else if (grade == ‘B’) {   tstmsg = “Good”; } else if (grade == ‘C’) {   tstmsg = “OK”; } else {   tstmsg = “Study More”; } 2.Write the following for statement as a while statement. for (k = 0; k < 3; k++) {   System.out.println...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT