ЁЯФД

JavaScript Lecture: Chapter 3 - Loops and Strings

May 30, 2024

JavaScript Lecture: Chapter 3 - Loops and Strings

Overview

  • Welcome to the Complete JavaScript Series
  • Covering JavaScript from zero to advanced
  • Today: Chapter 3 (Loops and Strings)
  • Topics: Loops and Strings, Practical Exercises, Project Applications

Resources

  • All lecture notes available in the description box
  • Access the full playlist for all JavaScript series lectures and practice questions

Loops in JavaScript

Introduction to Loops

  • Essential for repeating tasks in programming
  • Executes a piece of code repeatedly
  • Examples: Playing a song on loop, repeating actions

Types of Loops

  1. For Loop
  2. While Loop
  3. Do-While Loop
  4. For-of Loop
  5. For-in Loop

For Loop

  • Syntax: for (initialization; stopping condition; updation) { block of code }
  • Example: for (let count = 1; count <= 5; count++) { console.log("Apna College"); }
  • Steps:
    • Initialization: let count = 1
    • Stopping Condition: count <= 5
    • Updation: count++
    • Block of Code: console.log("Apna College")

While Loop

  • Syntax: while (condition) { block of code }
  • Example: let i = 1; while (i <= 5) { console.log("Apna College"); i++; }
  • Executes block of code until the condition is false

Do-While Loop

  • Syntax: do { block of code } while (condition);
  • Guarantees the loop runs at least once
  • Example: let i = 0; do { console.log("Apna College"); i++; } while (i < 5);

For-of Loop

  • Used to iterate over iterable objects (like arrays or strings)
  • Syntax: for (let variable of iterable) { block of code }
  • Example: let str = "JavaScript"; for (let char of str) { console.log(char); }
  • Ideal for strings and arrays

For-in Loop

  • Used for iterating over object properties
  • Syntax: for (let key in object) { block of code }
  • Example: let student = { name: "Rahul", age: 20, isPass: true }; for (let key in student) { console.log(key + ": " + student[key]); }

Practical Examples and Exercises

Example 1: Sum of First n Numbers

  • Problem: Calculate the sum from 1 to n
  • Solution: Using For Loop let sum = 0; let n = 5; for (let i = 1; i <= n; i++) { sum += i; } console.log("Sum: " + sum); // Sum: 15

Example 2: Print Even Numbers from 0 to 100

  • Problem: Print all even numbers between 0 to 100
  • Solution: Using For Loop and Conditional Check for (let num = 0; num <= 100; num++) { if (num % 2 === 0) { console.log(num); } }

Example 3: Simple Number Guessing Game

  • Problem: Create a game where users guess a number until correct
  • Solution: Using While Loop and Prompt let gameNumber = 25; let userGuess; while (userGuess != gameNumber) { userGuess = prompt("Guess the game number:"); if (userGuess == gameNumber) { console.log("Congratulations, you guessed the right number!"); } else { console.log("Wrong guess! Try again."); } }

Strings in JavaScript

What are Strings?

  • Sequence of characters used to represent text
  • Creation: let str1 = "Hello"; // double quotes let str2 = 'World'; // single quotes
  • Can also use backticks for Template Literals let str3 = `Template Literals`;

Important String Properties and Methods

  • length: Returns the length of the string let str = "Hello"; console.log(str.length); // 5
  • toUpperCase(): Converts string to uppercase console.log(str.toUpperCase()); // HELLO
  • toLowerCase(): Converts string to lowercase console.log(str.toLowerCase()); // hello
  • trim(): Removes whitespace from both ends of string let str = " Hello World! "; console.log(str.trim()); // "Hello World!"
  • slice(): Extracts a part of the string let str = "Hello World"; console.log(str.slice(0, 5)); // Hello
  • concat(): Combines two or more strings let str1 = "Hello"; let str2 = "World"; let result = str1.concat(" ", str2); console.log(result); // Hello World // Or simply using + console.log(str1 + " " + str2); // Hello World
  • replace(): Replaces a substring within string let str = "Hello"; console.log(str.replace("H", "Y")); // Yello
  • charAt(): Returns the character at specified index let str = "Hello"; console.log(str.charAt(1)); // e

Practical String Example: Generate Username

Problem

  • Prompt user to enter full name without spaces
  • Generate a username in the format @fullname[length]

Solution

let fullName = prompt("Enter your full name without spaces:"); let userName = "@" + fullName + fullName.length; console.log(userName);

Conclusion

  • Covered essential concepts of Loops and Strings in JavaScript
  • Practiced with relevant examples and exercises
  • Encouraged to explore more through additional resources