Data Operators
A review on some of the different operators.
Operators
Operators are symbols that perform actions on operands, which are values or variables. These allow you to edit data types.
Mathematical Operators
Mathematical operators allow you to perform arithmetic operations with data values.
There are your conventional math operations
Addition
let sum = 1 + 2; // sum is 3
Subtraction
let difference = 2 - 1; // difference is 1
Multiplication
let product = 2 * 4; // product is 8
Division
let quotient = 6 / 3; // quotient is 2
Then there are some others
Modulus
This operator gives you the remainder of the division between two operands.
let remainder = 5 / 2; // remainder is 1
Exponentation
This operator raises the first operand to the power of the second.
let power = 2 ** 4; // power is 16
Increment
This operator increases the value of the operand by 1.
let value = 0;
value++; // value is now 1
Decrement
This operator decreases the value of the operand by 1.
let value = 10;
value--; // value is now 9
Here is an example of some of them in code.
%% js
// additional ice for background
const image_src_icesheet = path + "/images/gamify/icesheet.png";
const image_data_icesheet = {
name: 'icesheet',
src: image_src_icesheet,
pixels: {height: 70, width: 107},
INIT_POSITION: { x: (width * 8 / 9), y: (height * 11 / 20) }, // multiplication
SCALE_FACTOR: 5
};
String Operators
String operators are able to manipulate different strings.
Concatenation
The concatentation operator (+) can combine strings.
%%javascript
let string1 = "Hello";
let string2 = "everyone";
let combination = string1 + " " + string2; // empty quotes for a space
console.log(combination); // prints Hello everyone
<IPython.core.display.Javascript object>
Template Literals
These use backticks (`) to be able to directly insert variables into a string by using ${variable name}.
%%javascript
let variable = "user"
let greeting = `Hello ${variable}`;
console.log(greeting);
<IPython.core.display.Javascript object>
Boolean Expressions
Boolean expressions are expressions that are either true or false. These can be essential in conditionals and loops.
Here is an example of some below.
%%javascript
loadLevel: function(targetLevelIndex = null) {
console.log('Entering loadLevel method')
// If targetLevelIndex is not empty/exists, then targetLevelIndex is set to currentLevelIndex
const levelIndex = targetLevelIndex !== null ? targetLevelIndex : this.currentLevelIndex; // strict inequality
// Greater than or equal to
// If levelIndex is greater than or equal to the length of levelClasses, run stopTimer() and then return
if (levelIndex >= this.levelClasses.length) {
this.stopTimer();
return;
}
GameEnv.continueLevel = true;
GameEnv.gameObjects = [];
this.currentPass = 0;
const LevelClass = this.levelClasses[levelIndex];
const levelInstance = new LevelClass(this.path);
// console.log('Created levelInstance:', levelInstance);
GameEnv.currentLevel = levelInstance;
this.loadLevelObjects(levelInstance);
this.transitionNPCS = [];
this.transitionNPCS = levelInstance.transitionNPCS || [];
console.log('Updated transitionNPCS in loadLevel:', this.transitionNPCS);
// strict equality
// If targetLevelIndex is empty then increment currentLevelIndex
if (targetLevelIndex === null) {
this.currentLevelIndex++;
}
}
Comparison Operators
Equal to : ==