You are on page 1of 18

Midterm Review: JS Control

Structures, misc.
Francisco (Best TA)
Control Flow
if else
Write a JavaScript program that reads a value representing a number of watts and
displays a message based on the following criteria:

● If the number of watts is between 10 (inclusive) and 50 (inclusive) the program


will print "low"
● If the number of watts is between 50 and 100 (inclusive) the program will print
"medium"
● If the number of watts is larger than 100 the program will print "high".
● For any other value, the program will print the message "Invalid Value”

Use the alert function to display the message.


Solution
switch
Let’s write a switch statement that takes in variable movie and logs to the
console your feelings of what movie is given (or that you haven’t seen it). What
could the default case log to the console?

Movies: “Interstellar”, “The Notebook”, “Avatar”


Solution
Loops
while
Let’s write a while loop that divides the variable braincells by 2 until
braincells reaches below 1.
Solution
for loop
Write a function that accepts two parameters: end and step. The function
should return the sum of every step numbers 0 through end. (Hint: use the %
operator.)

Example:

sum_function(10, 2) -> 0 + 2 + 4 + … + 10

sum_function(100, 40) -> 0 + 40 + 80


Solution
Misc.
HTML & JS
Write the HTML for a button that runs the function loadPage when clicked.
Solution
HTML & JS
Store the value of the input element with id = “userName” in a variable.

Set the value of the div element with id = “messageArea” with a greeting
using the value calculated above. (Hint: use string c o n c a t e n a t i o n)
Solution
HTML & JS
Write a Javascript program that reads a number with
prompt and prints a table. The first column of the table should
be numbers from 1 up to (and including) the provided number.
The second column should be the square of each number. Your
program must use the message "Enter Value" to read the value
from the user. You don't need to use meaningful variable names
(although you should); however, you must have good
indentation. You don't need to write any CSS. Assume the CSS
for table borders has already been included in the code.
On the right is a table generated when the user
provides 5 as input. Remember that your program
must work for different values (not just 5).
Solution

You might also like