[go: up one dir, main page]

0% found this document useful (0 votes)
12 views2 pages

4 Loops

loops in javascript class notes

Uploaded by

monucse508
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views2 pages

4 Loops

loops in javascript class notes

Uploaded by

monucse508
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Loops

10 November 2024 09:55

Loop Type Syntax Description Use Cases Example


for Loop for (initialization; Repeats a block of code a specific number of When the number for (let i = 0; i < 5; i++)
condition; times based on a condition. of iterations is { console.log(i); }
increment) { } known.
while Loop while (condition) { } Executes a block of code as long as a specified When the number let i = 0; while (i < 5)
condition is true. of iterations is { console.log(i); i++; }
unknown.
do...while do { } while Similar to while, but executes the code block at When you want to let i = 0; do { console.log(i);
Loop (condition); least once before checking the condition. run code at least i++; } while (i < 5);
once.
for...of for (variable of Iterates over iterable objects like arrays, Accessing elements for (const num of [1, 2, 3])
Loop iterable) { } strings, maps, sets, and more, accessing each in arrays or { console.log(num); }
element directly. collections.
for...in for (variable in Iterates over enumerable properties of an Accessing const obj = {a: 1, b: 2}; for
Loop object) { } object, typically used for iterating over object properties in an (const key in obj)
properties. object. { console.log(key); }
forEach array.forEach((elem A method that executes a provided function When iterating over [1, 2, 3].forEach((num) =>
Loop ent, index) => { }) once for each array element, but does not arrays with a console.log(num));
support break or continue. callback.
break Used inside loops Exits the loop entirely, skipping any remaining Exiting a loop early for (let i = 0; i < 5; i++) { if (i
Statement (e.g., for, while, iterations. based on a === 3) break;
etc.) condition. console.log(i); }
continue Used inside loops Skips the current iteration and continues with Skipping an for (let i = 0; i < 5; i++) { if (i
Statement (e.g., for, while, the next one. iteration based on a === 3) continue;
etc.) condition. console.log(i); }

Practice Problems:

Basic Problems
1. Print Numbers
Write a loop to print numbers from 1 to 20.
2. Sum of Array Elements
Given an array of numbers [2, 4, 6, 8, 10], calculate the sum of all elements in the array.
3. Reverse String
Write a function that takes a string as input and returns the string reversed.
○ Example: "hello" should return "olleh".
4. Count Down
Write a loop that counts down from 10 to 1 and prints each number.

Intermediate Problems
1. Find Minimum Value
Given an array of numbers [5, 3, 9, 1, 4], find and print the smallest value in the array.
2. Multiply Each Element by 3
Given an array [1, 2, 3, 4, 5], create a new array where each element is multiplied by 3. Print the new
array.
3. Character Frequency in a String
Write a function that takes a string and returns an object with the frequency of each character in the
string.
○ Example: "apple" should return { a: 1, p: 2, l: 1, e: 1 }.
4. Filter Out Long Words
Given an array of words ["dog", "elephant", "cat", "hippopotamus"], create a new array with only the
words that have more than 3 letters.

Advanced Problems

FrontEnd Page 1
Advanced Problems
1. Prime Numbers in an Array
Write a function that takes an array of numbers and returns a new array containing only the prime
numbers from the original array.
2. Find First Duplicate
Given an array of numbers [3, 5, 2, 4, 3, 6, 2], find and return the first duplicate number in the array. If
there’s no duplicate, return null.
3. Fibonacci Sequence
Write a function to generate and print the first 10 numbers in the Fibonacci sequence (0, 1, 1, 2, 3, 5, …).
4. Count Even and Odd Numbers
Given an array of numbers [7, 2, 9, 4, 6], count and return the number of even and odd numbers in the
array as an object.
○ Example: { even: 3, odd: 2 }.
5. Sum of Digits
Write a function that takes a number as input and returns the sum of its digits.
○ Example: sumOfDigits(123) should return 6 (1 + 2 + 3).
6. Remove Duplicates from Array
Write a function that removes duplicate values from an array and returns a new array with unique
elements.
○ Example: [1, 2, 2, 3, 4, 4, 5] should return [1, 2, 3, 4, 5].
7. Cumulative Sum of Array
Given an array [1, 2, 3, 4], create a new array where each element is the cumulative sum up to that
index.
○ Example: [1, 3, 6, 10].

FrontEnd Page 2

You might also like