Day 1: Introduction to JavaScript
Code Examples:
javascript// Example 1: Hello, World!
console.log('Hello, World!');
// Example 2: Writing JavaScript in an HTML file
// Save this code in an HTML file (index.html):
<!DOCTYPE html>
<html>
<head>
<title>JavaScript Intro</title>
</head>
<body>
<h1>Welcome to JavaScript!</h1>
<script>
console.log('Hello from the browser console!');
alert('Welcome to JavaScript!');
</script>
</body>
</html>
Day 2: Variables and Data Types
Code Examples:
javascript// Example 1: Variable declarations
let name = "Alice";
const age = 25;
var isStudent = true;
console.log(name, age, isStudent);
// Example 2: Data types
let number = 42; // number
let greeting = "Hello"; // string
let isHappy = true; // boolean
let nothing = null; // null
let notDefined; // undefined
let person = { firstName: "John", lastName: "Doe" }; // object
console.log(typeof number); // number
console.log(typeof greeting); // string
console.log(typeof isHappy); // boolean
console.log(typeof nothing); // object (quirk of JavaScript)
console.log(typeof notDefined); // undefined
console.log(typeof person); // object
Practice Task:
- Declare variables for a fictional character (name, age, occupation, isAlive). Use
console.log
to print a summary.
Day 3: Operators and Expressions
Code Examples:
javascript// Example 1: Arithmetic operators
let x = 10;
let y = 5;
console.log(x + y); // Addition
console.log(x - y); // Subtraction
console.log(x * y); // Multiplication
console.log(x / y); // Division
console.log(x % y); // Modulus
// Example 2: Comparison operators
console.log(x > y); // true
console.log(x < y); // false
console.log(x == "10"); // true (loose equality)
console.log(x === "10"); // false (strict equality)
// Example 3: Logical operators
let a = true;
let b = false;
console.log(a && b); // false (AND)
console.log(a || b); // true (OR)
console.log(!a); // false (NOT)
Practice Task:
- Write a program that takes two numbers and outputs whether the first is greater than, less than, or equal to the second.
Day 4: Control Flow (if-else, switch)
Code Examples:
javascript// Example 1: if-else statement
let number = 10;
if (number > 0) {
console.log("The number is positive.");
} else if (number < 0) {
console.log("The number is negative.");
} else {
console.log("The number is zero.");
}
// Example 2: switch statement
let day = 3;
switch (day) {
case 1:
console.log("Monday");
break;
case 2:
console.log("Tuesday");
break;
case 3:
console.log("Wednesday");
break;
default:
console.log("Invalid day");
}
Practice Task:
- Write a program that asks for a user's favorite fruit (e.g., apple, banana, orange) and responds with a specific message using a
switch
statement.
This code provides engaging examples and tasks to reinforce the concepts. Let me know if you need further explanation or additional examples!
You said:
now give me all day step by step code
ChatGPT said:
Here’s a step-by-step JavaScript code plan for all 15 days, following the lesson plan I provided earlier. Each day's code is accompanied by a task for students to practice.
Day 1: Introduction to JavaScript
javascript// Console message
console.log('Hello, World!');
// Browser alert
alert('Welcome to JavaScript!');
Practice Task:
- Open the browser console and log your name and age.
Day 2: Variables and Data Types
javascript// Variable examples
let name = "Alice";
const age = 30;
var isEmployed = true;
// Data types
console.log(typeof name); // string
console.log(typeof age); // number
console.log(typeof isEmployed); // boolean
// Undefined and null
let notDefined;
let empty = null;
console.log(typeof notDefined); // undefined
console.log(typeof empty); // object
Practice Task:
- Create variables to store a book's title, author, price, and availability.
Day 3: Operators and Expressions
javascript// Arithmetic operations
let a = 10;
let b = 5;
console.log(a + b); // 15
console.log(a - b); // 5
// Comparison
console.log(a > b); // true
console.log(a === b); // false
// Logical operators
console.log(a > b && b > 0); // true
console.log(a < b || b > 0); // true
console.log(!false); // true
Practice Task:
- Create a program to check if a user is eligible to vote based on their age.
Day 4: Control Flow
javascript// if-else example
let score = 75;
if (score >= 90) {
console.log("Grade: A");
} else if (score >= 75) {
console.log("Grade: B");
} else {
console.log("Grade: C");
}
// switch example
let day = 2;
switch (day) {
case 1:
console.log("Monday");
break;
case 2:
console.log("Tuesday");
break;
default:
console.log("Invalid day");
}
Practice Task:
- Write a program that checks if a number is even or odd.
Day 5: Loops
javascript// For loop
for (let i = 1; i <= 5; i++) {
console.log(`Count: ${i}`);
}
// While loop
let j = 1;
while (j <= 3) {
console.log(`While Count: ${j}`);
j++;
}
Practice Task:
- Write a program to print the multiplication table of 7.
Day 6: Functions
javascript// Function declaration
function greet(name) {
return `Hello, ${name}!`;
}
// Arrow function
const add = (x, y) => x + y;
console.log(greet("Alice"));
console.log(add(5, 3));
Practice Task:
- Write a function to calculate the square of a number.
Day 7: Arrays
javascript// Array methods
let numbers = [1, 2, 3, 4];
numbers.push(5);
console.log(numbers);
let squared = numbers.map(num => num ** 2);
console.log(squared);
Practice Task:
- Create an array of names and print each name in uppercase.
Day 8: Objects
javascript// Object example
let car = {
brand: "Tesla",
model: "Model S",
start() {
console.log("Car is starting...");
}
};
console.log(car.brand);
car.start();
Practice Task:
- Create an object for a person with properties like name, age, and greet method.
Day 9: DOM Manipulation
javascript// HTML: <button id="btn">Click me</button>
document.getElementById('btn').addEventListener('click', () => {
alert('Button clicked!');
});
Practice Task:
- Add an input field and a button. Show the input value in an alert when the button is clicked.
Day 10: Event Handling
javascript// HTML: <input id="input" /><button id="btn">Submit</button>
document.getElementById('btn').addEventListener('click', () => {
let value = document.getElementById('input').value;
alert(`You entered: ${value}`);
});
Practice Task:
- Create a form with validation for non-empty input fields.
Day 11: Async JavaScript
javascript// Fetch API example
fetch('https://jsonplaceholder.typicode.com/posts/1')
.then(response => response.json())
.then(data => console.log(data))
.catch(err => console.error(err));
Practice Task:
- Fetch and display the user information from an API.
Day 12: ES6+ Features
javascript// Destructuring
let person = { name: "Alice", age: 25 };
let { name, age } = person;
console.log(name, age);
// Spread operator
let nums = [1, 2, 3];
let newNums = [...nums, 4];
console.log(newNums);
Practice Task:
- Refactor previous tasks using ES6+ features.
Day 13: To-Do List App
javascript// HTML structure: <ul id="todoList"></ul><input id="todoInput" /><button id="addBtn">Add</button>
document.getElementById('addBtn').addEventListener('click', () => {
let input = document.getElementById('todoInput').value;
let list = document.getElementById('todoList');
let li = document.createElement('li');
li.textContent = input;
list.appendChild(li);
});
Practice Task:
- Add delete functionality for each task.
Day 14: Quiz App
javascript// Simple Quiz Example
let questions = [
{ question: "What is 2 + 2?", answer: 4 },
{ question: "What is the capital of France?", answer: "Paris" }
];
questions.forEach(q => {
let userAnswer = prompt(q.question);
console.log(userAnswer == q.answer ? "Correct!" : "Wrong!");
});
Practice Task:
- Add a score counter to the quiz.
Day 15: Wrap-Up and Q&A
- Review all concepts and encourage students to ask questions.
- Discuss potential projects like a portfolio site or a simple game.