Lesson 4
Lesson 4 of 16
Operators
Lesson Progress: 0%
Code Example
let sum = 5 + 3;
console.log(sum); // the answer should be 8
let result = 10 - 4;
console.log(result); // the answer should be 6
let product = 6 * 2;
console.log(product); // 12
let quotient = 20 / 5;
console.log(quotient); // 4Instructions
▼ ← Click the triangle to hide or reveal instructions.JavaScript Code Editor
Task Incomplete
Editor Input:
Editor Output:
append() helper.fruits.append("cherry") puts a cherry at the very end of your fruit list.Code Example
let remainder = 10 % 3;
console.log(remainder); // the answer should be 1
// Sharing 7 slices of pizza with 2 people
let pizzaLeftover = 7 % 2;
console.log(pizzaLeftover); // the answer should be 1
// Putting 12 toys into 4 equal boxes
let toyLeftover = 12 % 4;
console.log(toyLeftover); // 0Instructions
▼ ← Click the triangle to hide or reveal instructions.JavaScript Code Editor
Task Incomplete
Editor Input:
Editor Output:
extend() that lets you pour one list into another.Code Example
let x = 7;
console.log(x); // the answer should be 7
// Keeping track of a player's lives
let lives = 3;
console.log(lives); // the answer should be 3
// Counting how many gold coins you found
let coins = 50;
console.log(coins); // the answer should be 50Instructions
▼ ← Click the triangle to hide or reveal instructions.JavaScript Code Editor
Task Incomplete
Editor Input:
Editor Output:
lives to store the number 3 so the computer remembers it.console.log, we are telling the computer to "read out loud" what is in the box.Code Example
console.log(5 == 5); // true
// Example 1: Comparing a number and a string
console.log(10 == 10); // true
console.log(5 == "5"); // true
// Example 1: Comparing a number and a string
console.log(10 == "10"); // true
// Example 2: Comparing the number zero and an empty-looking string
console.log(0 == ""); // trueInstructions
▼ ← Click the triangle to hide or reveal instructions.JavaScript Code Editor
Task Incomplete
Editor Input:
Editor Output:
== is like a "close enough" check that looks at the meaning of the data.10 and the word "10" are twins, even though one is wearing "quote" clothes.0 and an empty message "" are the same thing!=== for a much stricter "exact match" test.Code Example
const b = 5;
console.log(5 === b); // true
const c = 10;
console.log(10 === c); // true
console.log(5 === "5"); // false
// Example 1: Strict check between a number and a string
console.log(10 === "10"); // false
// Example 2: Strict check between a number and a boolean
console.log(1 === true); // falseInstructions
▼ ← Click the triangle to hide or reveal instructions.JavaScript Code Editor
Task Incomplete
Editor Input:
Editor Output:
=== is a strict judge that requires things to be exactly the same.false for 10 === "10" because a plain number is not the same as a word.Code Example
let count = 5;
count++;
console.log(count); // 6
// Giving a player a point in a video game
let score = 10;
score++;
console.log(score); // 11
// Counting how many levels you finished
let level = 1;
level++;
console.log(level); // 2Instructions
▼ ← Click the triangle to hide or reveal instructions.JavaScript Code Editor
Task Incomplete
Editor Input:
Editor Output:
++ symbol is a shortcut that tells the computer to "add one" to your number.++ after the name to grow the number.Code Example
let count = 5;
count--;
console.log(count); // 4
// Losing a life in a game
let hearts = 3;
hearts--;
console.log(hearts); // 2
// Counting down the remaining cookies
let cookiesInJar = 10;
cookiesInJar--;
console.log(cookiesInJar); // 9Instructions
▼ ← Click the triangle to hide or reveal instructions.JavaScript Code Editor
Task Incomplete
Editor Input:
Editor Output:
-- symbol is a quick shortcut that tells the computer to "subtract one" from your number.Code Example
const guests = 5;
const meals = 4;
console.log(persons > seats);
// Checking if you have enough gold to buy a sword
const swordPrice = 10;
const myGold = 8;
console.log(myGold > swordPrice);
// Checking if a car can fit under a bridge
const bridgeHeight = 12;
const carHeight = 9;
console.log(carHeight < bridgeHeight);Instructions
▼ ← Click the triangle to hide or reveal instructions.JavaScript Code Editor
Task Incomplete
Editor Input:
Editor Output:
< and > symbols are like "Comparison Crocodiles" that check which number is bigger.true or false answer based on whether the rule you wrote is correct.Code Example
const guests = 5;
const meals = 4;
console.log(guests > meals);
// Checking if you have enough fuel for your rocket
const fuelNeeded = 100;
const fuelLevel = 150;
console.log(fuelLevel > fuelNeeded);
// Checking if your backpack is too heavy
const maxWeight = 20;
const currentWeight = 25;
console.log(currentWeight > maxWeight);Instructions
▼ ← Click the triangle to hide or reveal instructions.JavaScript Code Editor
Task Incomplete
Editor Input:
Editor Output:
> symbol is like a hungry crocodile that always wants to open its mouth toward the bigger number.true answer.Code Example
const leashes = 7;
const puppies = 3;
console.log(leashes >= puppies);
// Checking if you have enough fuel for your rocket
const fuelNeeded = 100;
const fuelLevel = 150;
console.log(fuelLevel >= fuelNeeded);
// Checking if your backpack is too heavy
const maxWeight = 20;
const currentWeight = 25;
console.log(currentWeight >= maxWeight);Instructions
▼ ← Click the triangle to hide or reveal instructions.JavaScript Code Editor
Task Incomplete
Editor Input:
Editor Output:
>= symbol checks if the first number is bigger than OR exactly the same as the second one.true as long as you have enough for everyone, even if it's a perfect fit.Code Example
const cars = 2;
const parkingSpaces = 5;
console.log(cars <= parkingSpaces);
// Can your group fit on a bench made for 3 people?
const people = 3;
const benchSeats = 3;
console.log(people <= benchSeats); // true (A perfect fit!)
// Is your suitcase light enough for the airplane?
const maxWeight = 50;
const myBagWeight = 42;
console.log(myBagWeight <= maxWeight); // true (It's under the limit!)Instructions
▼ ← Click the triangle to hide or reveal instructions.JavaScript Code Editor
Task Incomplete
Editor Input:
Editor Output:
<= symbol checks if a number is smaller than or exactly equal to another number.true as long as you don't go over the maximum allowed amount.Code Example
const cars = 2;
cars += 1;
console.log(cars);
cars += 3;
console.log(cars)
// Example 1: Gaining experience points (XP) in a game
let xp = 100;
xp += 50;
console.log(xp); // 150
// Example 2: Adding items to a shopping cart
let cartTotal = 2;
cartTotal += 1;
console.log(cartTotal); // 3
cartTotal += 5;
console.log(cartTotal); // 8Instructions
▼ ← Click the triangle to hide or reveal instructions.JavaScript Code Editor
Task Incomplete
Editor Input:
Editor Output:
+= symbol is a power-up that adds a new number to the current total and saves it.cars = cars + 1 by combining the math and the saving into one step.Code Example
const cars = 25;
cars -= 1;
console.log(cars);
cars -= 3;
console.log(cars);
// Example 1: Spending gold coins in a shop
let gold = 100;
gold -= 20;
console.log(gold); // 80
// Example 2: Fuel leaking from a gas tank
let fuel = 50;
fuel -= 5;
console.log(fuel); // 45
fuel -= 10;
console.log(fuel); // 35Instructions
▼ ← Click the triangle to hide or reveal instructions.JavaScript Code Editor
Task Incomplete
Editor Input:
Editor Output:
-= symbol is a shortcut that subtracts a number from the total and saves the new result.