JavaScript Control Structures
What is the proper operator for "not equals" in JavaScript?
!=
What symbol represents the and operator in JavaScript?
&&
How can we check if a variable num is even?
(num % 2 == 0) will be true
How many times will this program print "hello"? var i = 10; while (i > 0) { println("hello"); i--; }
10
What will be the value of sum after this code runs? var START = 1; var END = 4; function start(){ var sum = 0; for(var i = START; i <= END; i++){ sum += i; } }
10
In the following code, what will be the last number to print to the screen before the program finishes? for (var i = 0; i < 10; i++) { if (i % 2 == 0) { println(i); } else { println(2 * i); } }
18
How many possible values can Randomizer.nextBoolean() return?
2
How many times will the following code print "hello"? for (var i = 0; i < 8; i += 2) { println("hello"); }
4
How many times will the following program print "hello"? for (var i = 0; i < 5; i++) { println("hello"); }
5
How many times will the following code print "hello"? for (var i = 3; i < 8; i++) { println("hello"); }
5 **YES AGAIN
What is the proper way to compare if two values are equal in a boolean expression in JavaScript?
==
Why do we use constant variables?
All of the Above
Which of the following returns a random number between 1 and 10?
Randomizer.nextInt(1, 10)
How many times will this program print "hello"? var i = 50; while (i < 100) { println("hello"); }
This code will loop infinitely
What statement can we use inside of a loop to end it?
break
4.5.4: Meme Text Generator
function start(){ for(var i = 0; i < 50; i++){ println("I will not come to school late."); } }
4.6.5: Count By Sevens
function start(){ for(var i = 0; i <= 500; i += 7){ println(i); } }
4.8.5: Lots of Dice
function start(){ for(var i = 0;i < 100;i++){ var roll = Randomizer.nextInt(1,6) println("You rolled a " + roll) } }
4.6.6: Powers of Two
function start(){ for(var i = 1; i <= 1000000; i *= 2){ println(i); } }
4.4.7: Teenagers
function start(){ var age = readInt("Age: "); if(age == 15){ println("Yes, you are a teenager."); }else{ println("No, you are not a teenager."); } }
4.3.5: Rolling Dice
function start(){ var diceOne = readInt("First Dice Roll? "); var diceTwo = readInt("Second Dice Roll? "); var rolledDoubles = diceOne == diceTwo; println("Got Doubles: " + rolledDoubles); }
4.2.7: School's Out
function start(){ var holiday = readBoolean("Is today a holiday? "); holiday = !holiday; var weekday = readBoolean("Is today a weekday? "); var school = holiday || weekday; println("Holiday: " + school); }
4.2.6: Can You Graduate?
function start(){ var isSenior = readBoolean("Are you a Senior?"); var hasCredits = readBoolean("Do you have enough credits?"); var canGraduate = isSenior && hasCredits; println("Can I graduate " + canGraduate); }
4.1.4: Do You Have a Cat?
function start(){ var myBoolean = true; var anotherBoolean = false; println(" Do You have a cat? " + true); }
4.3.6: All Star
function start(){ var pointsPerGame = readInt("Points per game? "); var reboundsPerGame = readInt("Rebounds per game? "); var assistsPerGame = readInt("Assists per game? "); var isAllStar = pointsPerGame >= 25 || pointsPerGame >= 10 && reboundsPerGame >= 10 && assistsPerGame >= 10; println("Is all star? " + isAllStar); }
4.4.8: Meal Planner
function start(){ var vegetarian = "Vegetarian"; var lactose = "lactose intolerant"; var restriction = readLine("Restriction: "); if(restriction == "vegetarian"){ println("Vegetarian: Veggie burger"); } if(restriction == "lactose"){ println("Lactose Intolerant: No Cheese"); } if (restriction == "none"){ println("No restrictions: No alterations"); } } ** IF THIS DOESNT WORK JUST RELOAD THE PAGE AND TRY AGAIN!!
4.10.4: Snake Eyes
function start(){ while(true) { var roll1 = Randomizer.nextInt(1,6); var roll2 = Randomizer.nextInt(1,6); if (roll1 && roll2 == 1){ break; } println("You rolled: " + roll1 + ", " + roll2); } }
What kind of statement allows us to run a block code only if a certain condition is true?
if statement
What kind of statement allows us to run one block of code if one condition is true, and a separate block of code otherwise?
if/else statement
What is the term for the value that signals the end of user input?
sentinel
After the following code runs, what is the value of isWeekend? var isSaturday = true; var isSunday = false; var isWeekend = isSaturday || isSunday;
true
4.9.5: Fibonacci
var MAX = 1000; function start(){ var firstNum = 0; var secondNum = 1; println("1"); while (firstNum + secondNum < MAX){ var answer = firstNum + secondNum; firstNum = secondNum; secondNum = answer; println(answer); } }
Which of the following is a good example of a proper constant variable name?
var MAX_VALUE = 100;
4.7.4: Better Sum
var MIN = readInt("What is the minimum number?"); var MAX = readInt("What is the maximum number?"); function start(){ var sum = 0; for(var i = MIN; i <= MAX; i++){ sum += i; } println("The sum was " + sum); }
4.7.5: Factorial
var N = 5; function start(){ var MIN = 4; var MAX = 15; var product = 4; for(var i = MIN; i <= MAX;i++){ product *= i; } println("The product was " + product); }
4.5.5: The Worm
var NUM_CIRCLES = 15; function start(){ for(var i = 0; i < 15; i++){ var circle = new Circle(NUM_CIRCLES); circle.setPosition(i, 200); circle.setColor(Color.grey); add(circle); } }
4.5.6: Caterpillar
var NUM_CIRCLES = 15; function start(){ for(var i = 0; i < 15; i++){ var circle = new Circle(NUM_CIRCLES); if (i % 2 == 0) { circle.setPosition(i, 200); circle.setColor(Color.red); add(circle); } else { circle.setPosition(i, 200); circle.setColor(Color.green); add(circle); } } }
4.10.5: Better Password Prompt
var SECRET = "abc123"; function start(){ while (true) var password = ("What is the password? "); if (password == "abc123") { break; } } }
4.7.6: All Dice Values
var SIDES_ON_DICE = 6; function start() { for(var i = 1; i <= SIDES_ON_DICE; i++){ for(var a = 1; a <= 6; a++){ println(i + "," + a) } } }
4.8.6: Random Color Square
var SIDE_LENGTH = 100; function start(){ var rect = new Rectangle(100,50); rect.setPosition = Randomizer.nextInt(60,150); rect.setColor = Randomizer.nextColor(); add(rect); }
4.9.4: Inventory
var STARTING_ITEMS_IN_INVENTORY = 20; function start(){ var numItems = STARTING_ITEMS_IN_INVENTORY; var numItems = STARTING_ITEMS_IN_INVENTORY; while(numItems > 0) { println("We have " + numItems + " in inventory."); var buy = readInt("How many would you like to buy? "); if (buy>numItems) { println("There is not enough in inventory for that purchase."); } else { numItems -= buy; println("We have " + numItems + " left."); } } println("All out!"); }
Which of the following is correct loop and a half structure?
while(true){ //code if(condition){ break; } //code }
Which of the following is not a valid value for a boolean?
yes
What symbol represents the or operator in JavaScript?
||