You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
π§± Programming Building Blocks
In this homework, you will practice the basic building blocks of JavaScript:
Variables
Conditionals
Loops
Functions
Each section includes:
A short explanation
A small example
A few exercises to practice
1οΈβ£ VARIABLES
π Explanation
Variables store information so we can use it later.
We create variables using let.
Variables can store:
Text (strings)
Numbers
True / False values (booleans)
πΉ Example
let name = "Ana";
console.log(name);
βοΈ Exercises
Exercise 1
Create a variable called age and store your age.
Print it to the console.
Example:
let number = 10;
console.log(number);
Exercise 2
Create a variable called city.
Change its value and print it again.
Example:
let color = "blue";
color = "red";
console.log(color);
Exercise 3
Create a variable called isStudent and store true or false.
Print it.
Example:
let isOnline = true;
console.log(isOnline);
2οΈβ£ CONDITIONALS
π Explanation
Conditionals allow the program to make decisions.
if runs when the condition is trueelse runs when the condition is false
πΉ Example
let age = 20;if (age >= 18) {
console.log("Adult");
} else {
console.log("Minor");
}
βοΈ Exercises
Exercise 4
Create a variable number.
If it is greater than 0, print "Positive".
Otherwise, print "Negative".
Example:
let score = 60;if (score >= 50) {
console.log("Pass");
} else {
console.log("Fail");
}
Exercise 5
Create a variable temperature.
If it is greater than 30, print "Hot".
Else print "Normal".
Example:
let speed = 80;if (speed > 100) {
console.log("Too fast");
} else {
console.log("Safe speed");
}
3οΈβ£ LOOPS
π Explanation
Loops repeat code automatically.
A for loop is used when we know how many times we want to repeat.
πΉ Example
for (let i = 1; i <= 3; i++) {
console.log(i);
}
βοΈ Exercises
Exercise 6
Use a loop to print numbers from 1 to 5.
Example:
for (let i = 1; i <= 3; i++) {
console.log("Hello");
}
Exercise 7
Use a loop to print your name 3 times.
Example:
for (let i = 1; i <= 2; i++) {
console.log("JS");
}
4οΈβ£ FUNCTIONS
π Explanation
Functions group code so we can reuse it.
A functionmust be called to run.
πΉ Example
functionsayHello() {
console.log("Hello");
}
sayHello();
βοΈ Exercises
Exercise 8
Create a functioncalled greet that prints "Hello".
Example:
functionshowMessage() {
console.log("Welcome");
}
Exercise 9
Create a functionthat receives a name and prints "Hello NAME".
Example:
functiongreetUser(user) {
console.log("Hi " + user);
}
greetUser("Ana");
Exercise 10
Create a functionthat receives a number and prints whether it is positive or negative.
Example:
functioncheckScore(score) {
if (score >= 50) {
console.log("Pass");
} else {
console.log("Fail");
}
}
β Final Notes
Focus on understanding, not speed
If something does not work, read your code slowly
Use console.log() to debug
Ask questions in class if you are stuck
π― Goal: Learn the fundamentals of JavaScript step by step.