4 Loops
4 Loops
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