String Operations
String operations are methods and techniques used to manipulate and work with strings. Common string operations include concatenation, slicing, finding substrings, and replacing parts of a string. These operations are essential for handling and processing text data.
Example:
const sprite_src_explorer = path + "/images/gamify/explorer.png";
const sprite_data_explorer = {
id: 'Explorer',
greeting: "Hi I am Matt, I love exploring the jungle and a great adventure!",
src: sprite_src_explorer
};
Explanation:
“sprite_src_explorer” concatenates the path variable with a string to make the file for the explorer’s sprite image
Mathematical Operations
Mathematical operations are used to perform arithmetic calculations on numbers. Common operations include addition, subtraction, multiplication, division, and modulus. These operations are fundamental for performing calculations and manipulating numerical data.
Example:
const EXPLORER_SCALE_FACTOR = 3;
const sprite_data_explorer = {
INIT_POSITION: { x: 0, y: height - (height / EXPLORER_SCALE_FACTOR) },
pixels: { height: 3456, width: 832 }
};
Explanation
The “INIT_POSITION” property uses math operators to determine the initial position of the explorer. It does this by using division (/) and subtraction (-).
Boolean Expressions
Boolean expressions are logical statements that evaluate to either true
or false
. They are used in conditional statements and loops to control the flow of a program. Common boolean expressions include comparisons (e.g., ==
, !=
, >
, <
) and logical operations (e.g., &&
, ||
, !
).
Example:
class Tree extends Character {
constructor(data) {
super(data);
this.isChopped = false;
}
chopTree() {
if (!this.isChopped) {
this.isChopped = true;
// Additional logic for chopping the tree
}
}
}
Explanation
The “chopTree” method uses a boolean expression to check if the tree has already been chopped (if(!this.isChopped)). If it isn’t chopped then it’s set to false.