Conditionals
Loops
Functions
Arrays
Miscellaneous
100
Will this print true or false? if (1 == 1){ alert("true"); }else{ alert("false"); }
true
100
What keyword can you use to immediately exit a while loop?
break
100
What function could you use to pop up a message to the user?
alert
100
Print your name by accessing the array var names = ["Bill", "Earl", "Elijah", "Lilibeth", "Raul"];
Depends on your name! 0 for Bill 1 for Earl 2 for Elijah 3 for Lilibeth 4 for Raul
100
Write two ways to increment a variable by one.
i++ i = i + 1; i += 1;
200
Will this print true or false? var class = "ScriptEd"; if("class" === "ScriptEd"){ alert("true"); }else{ alert("false"); }
false
200
Create an infinite loop
while(true){}
200
What function could you use to get input from the user?
prompt
200
Define an array that contains a number, a string, and an array.
var a = [1, "a string", ["an", "array"]];
200
In a CSS selector, how could I refer to an element that has id="example"?
#example
300
What will this print? var num = 25; if(num < 5){ alert("A"); }else if(num > 10){ alert("B"); }else if(num > 20){ alert("C"); }else if(num > 30){ alert("D"); }else{ alert("E"); }
B
300
What will this print? var sum = 0; var i = 0; while(i < 5){ sum = sum + i; i++; } alert(sum);
10
300
How would you use the "$" jQuery function to access every div element on a page?
$("div")
300
Print the length of an array called "someArray"
someArray.length
300
What are two ways to comment out code?
// for a single line /* for multiple lines */
400
Find 3 mistakes in this code! var x = prompt("Enter a number"); if(x = 3){ alert("You entered 3"); }if else(x > 3){ alert("You entered > 3"); else{ alert("You entered < 3"); }
1) "x = 3" should be "x === 3" 2) "if else" should be "else if" 3) There should be a "}" before "else"
400
Print all even numbers from 0 to 10 (including 0 and 10) using a while loop
var i = 0; while(i < 11){ if (i%2 == 0) { alert(i); } i++; }
400
What function could you use to get a random number? Double points if you use it to store a random WHOLE number between 0 and 10 (including 0 and 10) in a variable. You have 3 minutes!
Math.random() // Bonus var randomNumber = Math.floor(Math.random() * 10);
400
What are valid indexes for the following array? What gets returned if I access the array with an invalid index? var school = ["Harlem", "Village", "Academy"];
0, 1, and 2 undefined
400
Why have we been defining our JavaScript at the bottom of our html body element?
To make sure the html elements we're accessing in our JavaScript have already been declared.
500
Below is some code that asks the user for their test score. Add to it so it checks to make sure the score entered isn't below 0 or above 100. If it is below 0 or above 100, just print a message to the user saying "Invalid score". var testScore = parseInt(prompt("What was your test score?")); // Your code here
if(testScore > 100){ alert("Invalid score"); }else if(testScore < 0){ alert("Invalid score"); }
500
Given this array, print every element in it using a while loop var names = ["Bill", "Earl", "Elijah", "Lilibeth", "Raul"]; // Your code here
int i = 0; while(i < names.length){ alert(names[i]); i++; }
500
The code below prints "You know nothing of jelly bean jeopardy" even when the user enters 100. What function can I use to get the code to print "100 is correct!" when the user enters 100? Double points if you can make the appropriate change to the code. You have 3 minutes! var jellyBeans = prompt("How many jelly beans am I about to win?"); if(jellyBeans === 100){ alert("100 is correct!"); }else{ alert("You know nothing of jelly bean jeopardy"); }
parseInt // Bonus var jellyBeans = parseInt(prompt("How many jelly beans am I about to win?"));
500
Print the following array with its elements sorted. var numbers = [8,6,7,5,3,0,9]; Hint: You might need to do some Googling.
console.log(numbers.sort());
500
Write code to add your name to the following array var hackers = ["Bill Gates", "Mark Zuckerberg"] // Your code here
hackers.push("Andrew Ingraham");