Untitled Document
Untitled Document
Printing to Console
// Using the println() function will print the text and create a line
break
// Example of using println()
println("Hello");
println("world.");
// Result:
// Hello
// world.
// You can also print to the console using console.log() (includes line
break)
// Example:
console.log("Hello world!");
JavaScript
Variables
// Declare a variable
var myVarName;
// Initialize a variable
var myVarName = 5;
// Print a variable
println(myVarName);
println("The value is: " + myValue);
// Variables can also be declared and initialized using the keyword 'let'
let myVarName;
let myVarname = 5;
// If a variable isn't going to change its value, it is best to use the
keyword 'const'
/*
* Variables defined with const cannot be redeclared
* Variables defined with const cannot be reassigned
* Variables defined with const must be assigned a value when they are
declared
*/
// Example:
const PI = 3.141592653589793;
PI = 5; // This will give an error
JavaScript
User Input
// Read a string
// Strings are a series of characters - ex: Hello World
var str = readLine(prompt);
// Read an integer
// Integers are numbers without a decimal point - ex: 3
var num = readInt(prompt);
// Read a float
// Float are numbers with a decimal point - ex: 3.14
var cost = readFloat(prompt);
// Read a boolean
// Boolean are true/false
var bool = readBoolean(prompt);
JavaScript
JavaScript
To read more about asynchronous user input, see this tutorial.
Math
// Operators:
+ Addition
- Subtraction
* Multiplication
/ Division
** Exponentiation
% Modulus (Remainder)
() Parentheses (For order of operations)
// Examples
var z = x + y;
var w = x * y;
// Shortcuts
x = x + y; x += y;
x = x - y; x -= y;
x = x * y; x *= y;
x = x / y; x /= y;
// Exponentiation
var squared = 5 ** 2;
println(squared); // prints out 25
// Modulus
var z = 10 % 4 // 2 * 4 = 8; 10 - 8 = 2
println(z) // prints out: 2
// 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);
println(roundedPi); // prints out: 3
// Floor Division
// Math.floor() can be used to perform floor
// division. With floor division, only the
// integer portion of the quotient is returned.
// Geometry
// Note input is in radians, not degrees
JavaScript
Random Numbers
// Random integer between low and high
Randomizer.nextInt(low, high);
Randomizer.nextBoolean();
Randomizer.nextFloat(low, high);
Randomizer.nextColor();
Strings
// str.length
// returns the length of a string
// Example
var str = "hello";
var len = str.length; // equals 5
// str.indexOf(search)
// returns the first index of the search
// or -1 if not found. It is case sensitive.
//Examples
var str = "hello";
var pos1 = str.indexOf("l"); // returns 2
// str.substring(start);
// returns a substring including the
// character at start to the end of the
// string
//Examples
var str = "hello";
var sub1 = str.substring(1); // equals "ello"
var sub2 = str.substring(3); // equals "lo"
// str.substring(start, end);
// returns a substring including the
// character at start, but not including
// the character at end
//Examples
var str = "hello";
var sub1 = str.substring(0,2); // equals "he"
var sub2 = str.substring(1,4); // equals "ell"
JavaScript
Functions
// Functions can take in values, called parameters.
// The function below takes in a parameter called
// 'input' and prints it.
function printText(input) {
println(input);
}
JavaScript
Control Structures
Booleans
// A boolean is either true or false
var myBoolean = true;
// Not Operator
var x = !y; // x gets the opposite of y
// And Operator
var andExp = x && y;
// Or Operator
var orExp = x || y;
JavaScript
Logical Operators
// Logical operators return booleans (true/false values)
x && y // AND operator -- true if BOTH x and y are true
x || y // OR operator -- true if x OR y are true
! x // NOT operator -- true if x is false
if(x || y){
println("x and/or y are true");
}
JavaScript
Comparison Operators
// 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
JavaScript
} else if(condition_2) {
} else if(condition_3) {
} else {
JavaScript
For Loops
var COUNT = 5;
for(var i = 0; i < COUNT; i++){
/* Repeat code betweeen the brackets 5 times,
* as the COUNT variable is 5. */
}
JavaScript
While Loops
while(boolean expression){
/* Repeat code betweeen brackets while
* 'boolean expression' is true */
}
JavaScript
// This is a loop and a half format
while(true){
// code
if(condition){
break;
}
}
JavaScript
Data Structures
Arrays
// Create an empty array
var arr = [];
// Create an array with values
var arr = [1, 2, 4];
// length of an array
var length = arr.length;
// Add to an array
arr.push(elem);
JavaScript
Maps/Objects
// Object literal
var obj = {
name: "Jeremy",
color: "blue"
};
JavaScript
Sets
// Make a new set named "newSet"
var newSet = new Set();
// Add to a set
newSet.add(5);
// Call the intersect function on "setA" and pass in "setB", store the
resulting
// set in a new variable named "mutualSet"
var mutualSet = setA.intersect(setB);
JavaScript
Grids
// Create a grid named "newGrid"
var newGrid = new Grid(rows, cols);
// Getting dimensions
var rows = newGrid.numRows();
var cols = newGrid.numCols();