Learning Objectives
AAP-1.A.1 : A variable is an abstraction inside a program that can hold a value. Each variable has associated data storage that represents one value at a time, but that value can be a list or other collection that in turn contains multiple values.
AAP-1.A.2 : Using meaningful variable names helps with the readability of program code and understanding of what values are represented by the variables.
AAP-1.A.3 : Some programming languages provide types to represent data, which are referenced using variables. These types include numbers, Booleans, lists, and strings.
AAP-1.A.4 : Some values are better suited to representation using one type of datum rather than another.
AAP-1.B.1 : The assignment operator allows a program to change the value represented by a variable.
AAP-1.B.2 : The exam reference sheet provides the “ ” operator to use for assignment. For example, Text: a expression Block: a expression evaluates expression and then assigns a copy of the result to the variable a.
AAP-1.B.3 : The value stored in a variable will be the most recent value assigned. For example: a1 ba a2 display(b) still displays 1.
AAP-2.B.3 : An expression can consist of a value, a variable, an operator, or a procedure call that returns a value.
AAP-2.B.4 : Expressions are evaluated to produce a single value.
AAP-2.B.5 : The evaluation of expressions follows a set order of operations defined by the programming language.
AAP-2.C.1 : Arithmetic operators are part of most programming languages and include addition, subtraction, multiplication, division, and modulus operators.
AAP-2.C.2 : The exam reference sheet provides a MOD b, which evaluates to the remainder when a is divided by b. Assume that a is an integer greater than or equal to 0 and b is an integergreaterthan0. Forexample,17MOD 5 evaluates to 2.
AAP-2.C.3 : The exam reference sheet provides the arithmetic operators +, -, *, /, and MOD. Text and Block: §a + b §a – b §a * b §a / b §a MOD b These are used to perform arithmetic on a and b. For example, 17 / 5 evaluates to 3.4.
AAP-2.C.4 : The order of operations used in mathematics applies when evaluating expressions. The MOD operator has the same precedence as the * and / operators.
AAP-2.D.1 : String concatenation joins together two or more strings end-to-end to make a new string.
AAP-2.D.2 : A substring is part of an existing string.
AAP-3.E.1 : The exam reference sheet provides RANDOM(a, b)which generates and returns a random integer from a to b, inclusive. Each result is equally likely to occur. For example, RANDOM(1, 3) could return 1, 2, or 3.
AAP-3.E.2 : Using random number generation in a program means each execution may produce a different result.
Variables
//Declare a variable
var myVarName;
let myGlobalVarName;
// Initialize a variable
var myVarName = 5;
// Assign to an existing variable
myVarName = 10;
Data Types
Boolean variables
A boolean is either true or false
var myBoolean = true;
var anotherBoolean = false;
var result = prompt("Question? ");
Javascript Operators (Mathematical)
Operators:
+ Addition
- Subtraction
* Multiplication
/ Division
% Modulus (Remainder)
() Parentheses (For order of operations)
// Examples
var z = x + y;
var w = x * y;
// Increment (add one)
x++
// Decrement (subtract one)
x--
Shortcuts
x = x + y; x += y;
x = x - y; x -= y;
x = x * y; x *= y;
x = x / y; x /= y;
Other Common Mathematical Operations
// Absolute value
var abs = Math.abs(x);
// Square root
var sqrt = Math.sqrt(x);
// Rounding
// Math.round() can be used to round numbers
var pi = 3.14;
var roundedPi = Math.round(pi);
alert(roundedPi); // prints out: 3
var goldenRatio = 1.618;
var roundedGoldenRatio = Math.round(goldenRatio);
alert(roundedGoldenRatio); // prints out: 2
// Floor Division
// Math.floor() can be used to perform floor division. With floor division, only the
// integer portion of the quotient is returned.
// For example, 5/2 is 2.5, but with floor division, the result is 2 and
// the .5 is discarded.
var result = Math.floor(5/2);
alert(result); // prints out: 2
Comparison Operators (Boolean)
// Comparison operators return booleans (true/false values)
x == y // is x equal to y
x != y // is x not equal to y
x> y // is x greater than y
x >= y // is x greater than or equal to y
x< y // is x less than y
x <= y // is x less than or equal to y
// Comparison operators in if statements
if(x == y){
alert("x and y are equal"); }
if(x > 5){
alert("x is greater than 5.");}
Logic Operators
Not Operator
NOT APCS P Language
! JS Language
var x = !y; // x gets the opposite of y
And Operator
AND APCS P Language
&& JS Language
var andExp = x && y;
Or Operator
OR APCS P Language
|| JS Language
var orExp = x || y;
// You can combine many booleans!
var boolExp = x && (y || z);