JavaScript Exercises: for...of
Loops with String and Number Manipulations
Explore these exercises designed to enhance your JavaScript skills through practical use of for...of
loops combined with string and number manipulations. A for...of
loop iterates over iterable objects such as arrays, strings, maps, node lists, and more. It allows you to access the value of each item directly, which is particularly useful for collections of data.
Syntax of for...of
loop
The basic syntax of a for...of
loop is shown below:
|
|
JavaScript Exercises: for...of
Loops with String and Number Manipulations
1. Sum of Array Elements
Question: Use a for...of
loop to sum all numbers in the array [10, 20, 30, 40]
.
Required Output: 100
2. Count Digits in Strings
Question: Use a for...of
loop to count all numeric characters in the string “User2023ID”.
Required Output: 4
3. Convert String to Title Case
Question: Use a for...of
loop to convert each word in the string “hello world” to title case.
Required Output: "Hello World"
4. Reverse Each Word
Question: Use a for...of
loop to reverse each word in the string “hello world”.
Required Output: "olleh dlrow"
5. List Odd Numbers
Question: Use a for...of
loop to print all odd numbers from the array [1, 2, 3, 4, 5, 6, 7, 8, 9]
.
Required Output: 1 3 5 7 9
6. Concatenate Numbers as Strings
Question: Use a for...of
loop to concatenate all numbers in [1, 2, 3]
as a single string.
Required Output: "123"
7. Find the Longest Word
Question: Use a for...of
loop to find the longest word in the array ["JavaScript", "HTML", "CSS", "Python"]
.
Required Output: "JavaScript"
8. Capitalize Vowels
Question: Use a for...of
loop to capitalize all vowels in the string “dynamic web applications”.
Required Output: "dynAmIc wEb ApplIcAtIOns"
9. Sum of Square of Numbers
Question: Use a for...of
loop to find the sum of the squares of numbers in the array [1, 2, 3, 4]
.
Required Output: 30
10. Create Acronym
Question: Use a for...of
loop to create an acronym from the array ["Graphics", "Interchange", "Format"]
.
Required Output: "GIF"
11. Filter Non-numeric Characters
Question: Use a for...of
loop to remove non-numeric characters from the string “a1b2c3”.
Required Output: "123"
12. Double Even Numbers
Question: Use a for...of
loop to double each even number in the array [1, 2, 3, 4, 5, 6]
.
Required Output: 2 4 6 8 12
13. Count Uppercase Letters
Question: Use a for...of
loop to count all uppercase letters in the string “Hello World”.
Required Output: 2
14. Reverse Numbers in Array
Question: Use a for...of
loop to reverse the digits of each number in the array [123, 456]
.
Required Output: 321 654
15. Find Smallest Number
Question: Use a for...of
loop to find the smallest number in the array [10, 20, 30, 40, 50]
.
Required Output: 10
By the end of this assignment, you should be able to use a for...of
loop to perform various string and number manipulation tasks in JavaScript.