What is a function?
Functions hold code that tell objects what to do.
What are if-statements used for?
if-statements run lines of code only if a condition is true. The condition is a Boolean value (true or false).
Give the code (a Boolean expression) for the following:
The variable num is greater than or equal to 8.
num >= 8
What is a variable?
A variable holds a value. It like a box that can store a piece of information inside of it.
What symbol goes at the end of (almost) every line - excluding control structures?
; (semicolon)
Give the code for the following:
Call the function called moveX on the ball object. The speed parameter is 50.
ball.moveX(50);
How do you format an if-statement?
if (condition) {
code;
}
What operator (symbol(s)) do you use to check if two values are equal?
== or ===
Give code for the following:
Create a variable called num that equals 10.
var num = 10;
What is the format for calling a function?
objectName.functionName(parameters);
What is a parameter and where are they written in a function?
A parameter is an additional value used in a function, like the speed to move an object in the moveY function. Parameters are written inside the parentheses and separated by , (commas).
object.function(parameter1, parameter2);
If I want to run code when an if-statement condition is false, what kind of statement should I add to my if-statement?
An else statement.
if (condition) {
code;
} else {
code;
}
How do you write the and, or, and not operators? (Write the symbols.)
and: &&
or: ||
not: !
What is the difference between a variable and a property?
A variable is local and doesn't belong to an object.
A property is global and belongs to an object.
What is wrong with the syntax of this code?
var x = 10;
if (x == 10) {
x = 2;
}
The line x = 2; should be indented to look like:
var x = 10;
if (x == 10) {
x = 2;
}
Give the code for the following:
Call the function called isTouching on the ball object. The parameter is the wall object.
ball.isTouching(wall);
Give pseudocode for this scenario:
If it's raining outside, I should take an umbrella with me. Otherwise, I'm going to wear flip-flops.
if (raining) {
take an umbrella;
} else {
wear flip-flops;
}
Give an example of a scenario where you would use a Boolean in code.
if-statement condition, while-loop condition, variable, etc.
If you want to have a score in your game, should you use a variable or a property?
Property
What is wrong with the syntax of this code?
if (ball.isTouching(wall)) {
ball.moveY(10);
The if-statement is missing a closing { (curly brace).
Give the code for the following:
Set the x position of the ninja object to 400.
ninja.x(400);
Give the code for the following:
If the variable num is equal to 4, then set num equal to 1. Otherwise, if the variable num is equal to 1, then set num equal to 4.
if (num == 4) {
num = 1;
} else if (num == 1) {
num = 4;
}
Give the code (a Boolean expression) for the following:
The variable num is less than 5 AND the variable right equals true.
num < 5 && right === true
OR
num < 5 && right
Give the code for the following:
Create a property called direction that belongs to the ball object. Set it equal to 50.
ball.speed = 50;
What is wrong with the syntax of this code?
var rightPressed = isKeyPressed(Keys.rightArrow);
if (rightpressed) {
ball.moveX(50);
}