Monday, December 18, 2023

Day 12 || For loop in JavaScript || While Loop in JavaScript || Loop through an Array and Object

JavaScript Topics

Day 12 :

Looping Statement:

  •     For Loop
  •     While Loop
  •     Loop through an Array and Object

For Loop:














for(var i = 0; i<=100; i++){
    console.log(i)
}

for(var number = 1; number<=5; number++){
    console.log(number)
}


While Loop:











var count = 0;
while(count<=5){
    console.log(count);
    count++
}

var number = 0;
var sum = 0;
while(number<=10){
    console.log("No: " + number)
    console.log("Sum: " + sum)
    sum = sum + number;
    console.log("sum + number: " + sum)
    number++;
    console.log("inc No: " + number)
}
console.log(sum)


Loop through an Array and Object:

Array:

var naturalNumber = [10, 20, 30, 40, 50];
for(var number=0; number<naturalNumber.length; number++){
    console.log(naturalNumber[number] + 2)
}


Object:

var magicalLibrary = {
  book1: {
    name: "Fantasy",
    author: "Eleanor Moon",
    keyPlayer: "Aria",
  },
  book2: {
    name: "Mystery",
    author: "Maxwell Nightingale",
    keyPlayer: "Detective Drake",
  },
  book3: {
    name: "Adventure",
    author: "Isabella Silverwing",
    keyPlayer: "Captain Hawk",
  },
  book4: {
    name: "Adventure",
    author: "Isabella Silverwing",
    keyPlayer: "Captain Hawk",
  }
};

for (var book in magicalLibrary) {
    console.log(magicalLibrary[book].name)
    console.log(magicalLibrary[book].keyPlayer)
}


Tasks:

Task 1: Multiplication Table (Using for Loop)
Write a program that takes a number as input and uses a for loop to generate the multiplication table for that number up to 10.

Task 2: Factorial Calculator (Using while Loop)
Write a program that prompts the user to enter a positive integer and uses a while loop to calculate its factorial.

Task 3: Find Even Numbers in an Array
Objective:
Write a program that iterates through an array of numbers and prints only the even numbers.
Instructions:
Create an array of numbers 
Use a for loop to iterate through the array.
Print only the even numbers to the console.

Task 4: Movie Database
Objective:
Create a program that represents a simple movie database using an object. The object should contain information about several movies.
Instructions:
Create an object named movieDatabase with at least three movie entries. 
Each movie entry should have the following properties:
Title
Director
Release Year
Genre
Rating (out of 10)
Use a for...in loop to iterate through the movieDatabase object.
For each movie, print its title, director, and rating to the console.

Interview Questions:

1. What is the difference between a for loop and a for...in loop?
2. Can you use multiple variables in the initialization, condition, and iteration expressions of a for loop?
3. How does a while loop differ from a for loop?
4. Can a while loop have multiple conditions in its condition expression?

No comments:

Post a Comment

Top 10 | JavaScript Coding Interview Question | Beginner Level

               JavaScript Coding Interview  Q. No. 01/10:  console . log ( "1" + "4" + "1" ) // 141 console . ...