JavaScript – Part 3: Exercises (Practice Guide + Concepts)
This is a structured JavaScript practice chapter focused on real coding exercises, logic building, and problem-solving. If you complete these exercises properly, you will move from beginner → solid intermediate level.
🧠 1. Variables, Data Types & Input Practice
JavaScript starts with understanding how data is stored and used.
🟢 Exercise 1: Personal Info Logger
Create variables for:
- Your name
- Age
- Country
- Favorite programming language
Then display them in console.
Task:
- Use
letorconst - Print output using
console.log()
Example:
let name = "Ali"; let age = 18; let country = "Pakistan"; let language = "JavaScript"; console.log(name); console.log(age); console.log(country); console.log(language);
🔥 Challenge:
Combine all values into one sentence:
"My name is Ali and I am 18 years old from Pakistan."
🟢 Exercise 2: Simple User Input Simulation
Since browser input is limited, simulate user data:
let username = "Zabi"; let email = "zabi@example.com";
Task:
- Print a formatted profile card in console
Output style:
--- USER PROFILE --- Name: Zabi Email: zabi@example.com --------------------
🔁 2. Conditions (if / else logic)
Conditions are the brain of programming.
🟡 Exercise 3: Even or Odd Checker
Task:
Create a number and check if it is even or odd.
let number = 10;
if (number % 2 === 0) {
console.log("Even Number");
} else {
console.log("Odd Number");
}
🔥 Challenge:
Modify it to check numbers up to 100.
🟡 Exercise 4: Age Verification System
Task:
Check if user is eligible for driving license.
Rules:
- 18+ → Allowed
- Below 18 → Not allowed
let age = 17;
if (age >= 18) {
console.log("You are eligible for driving.");
} else {
console.log("You are not eligible yet.");
}
🟡 Exercise 5: Grade Calculator
Task:
Based on marks, assign grade:
- 90–100 → A+
- 80–89 → A
- 70–79 → B
- 60–69 → C
- Below 60 → Fail
let marks = 85;
if (marks >= 90) {
console.log("A+");
} else if (marks >= 80) {
console.log("A");
} else if (marks >= 70) {
console.log("B");
} else if (marks >= 60) {
console.log("C");
} else {
console.log("Fail");
}
🔁 3. Loops (Very Important for Logic Building)
Loops help repeat tasks efficiently.
🟠 Exercise 6: Print Numbers 1 to 10
for (let i = 1; i <= 10; i++) {
console.log(i);
}
🟠 Exercise 7: Multiplication Table
Task:
Print table of 5.
let num = 5;
for (let i = 1; i <= 10; i++) {
console.log(num + " x " + i + " = " + (num * i));
}
🟠 Exercise 8: Sum of Numbers
Task:
Find sum of 1 to 100.
let sum = 0;
for (let i = 1; i <= 100; i++) {
sum = sum + i;
}
console.log(sum);
🟠 Exercise 9: Reverse Counting
for (let i = 10; i >= 1; i--) {
console.log(i);
}
🧩 4. Functions (Reusable Logic)
Functions are reusable blocks of code.
🔵 Exercise 10: Simple Greeting Function
function greet(name) {
console.log("Hello " + name);
}
greet("Ali");
greet("Zabi");
🔵 Exercise 11: Add Two Numbers
function add(a, b) {
return a + b;
}
console.log(add(5, 10));
🔵 Exercise 12: Calculator Function
function calculator(a, b, operator) {
if (operator === "+") return a + b;
if (operator === "-") return a - b;
if (operator === "*") return a * b;
if (operator === "/") return a / b;
}
console.log(calculator(10, 5, "+"));
🧱 5. Arrays (Data Collections)
Arrays store multiple values.
🟣 Exercise 13: Array Basics
let fruits = ["Apple", "Banana", "Mango"]; console.log(fruits[0]); console.log(fruits[1]); console.log(fruits[2]);
🟣 Exercise 14: Loop Through Array
for (let i = 0; i < fruits.length; i++) {
console.log(fruits[i]);
}
🟣 Exercise 15: Find Largest Number
let numbers = [10, 50, 30, 90, 20];
let max = numbers[0];
for (let i = 1; i < numbers.length; i++) {
if (numbers[i] > max) {
max = numbers[i];
}
}
console.log("Largest:", max);
🧠 6. Mini Projects (Real Practice)
🟢 Project 1: Simple Login System
let username = "admin";
let password = "1234";
if (username === "admin" && password === "1234") {
console.log("Login Successful");
} else {
console.log("Login Failed");
}
🟢 Project 2: Number Guessing Game
let secret = 7;
let guess = 5;
if (guess === secret) {
console.log("Correct!");
} else {
console.log("Try Again!");
}
🟢 Project 3: Shopping Bill Calculator
let price1 = 100;
let price2 = 200;
let price3 = 150;
let total = price1 + price2 + price3;
console.log("Total Bill:", total);
🚀 FINAL PRACTICE CHALLENGE (IMPORTANT)
Create a program that:
- takes a number
- checks if it is even or odd
- prints multiplication table
- calculates sum up to that number
If you can do this, you are already intermediate level JavaScript developer.
🧠 WHAT YOU LEARNED
You practiced:
✔ Variables
✔ Conditions
✔ Loops
✔ Functions
✔ Arrays
✔ Mini projects