Javascript Exercises using for, for...of, and for...in loops -4

Intermediate JavaScript Logic Exercises

Explore intermediate JavaScript exercises focusing on various loop constructs and their practical applications. These exercises will challenge your understanding of for, for...of, and for...in loops, enhancing your ability to manipulate arrays, strings, and objects efficiently.

1. For Loop - Sum of Numbers

Question: Write a for loop to calculate the sum of numbers from 1 to 100.

Hint: Use the + operator to add numbers.

Required Output: 5050

2. For…of Loop - Iterate and Transform

Question: Use a for...of loop to iterate through an array of numbers [5, 10, 15] and multiply each number by 2, then print each result.

Hint: Use the * operator to multiply numbers.

Required Output: 10 20 30

3. For…in Loop - Print Property Values

Question: Use a for...in loop to print all values of the object {a: 1, b: 2, c: 3}.

Hint: Use the console.log function to print values.

Required Output: 1 2 3

4. For Loop - Count Even Numbers

Question: Write a for loop that counts how many even numbers there are from 1 to 50.

Hint: Use the modulo operator % to check if a number is even or odd.

Required Output: 25

5. For…of Loop - Concatenate Strings

Question: Use a for...of loop to concatenate all strings in the array ["Hello", "world", "!"].

Hint: Use the + operator to concatenate strings.

Required Output: Hello world !

6. For…in Loop - Sum Object Values

Question: Use a for...in loop to sum all numeric values in the object {x: 10, y: 20, z: 30}.

Hint: Use the + operator to add numbers.

Required Output: 60

7. For Loop - Generate Fibonacci Sequence

Question: Write a for loop to generate the first 7 numbers of the Fibonacci sequence.

Hint: Use the + operator to add numbers.

Required Output: 0 1 1 2 3 5 8

8. For…of Loop - Find Maximum Value

Question: Use a for...of loop to find the maximum number in the array [3, 7, 2, 5, 6].

Required Output: 7

9. For…in Loop - Copy Properties

Question: Use a for...in loop to copy properties from {a: 1, b: 2} to a new object and print the new object.

Hint: Use the Object.assign function to copy properties from one object to another.

Required Output: {a: 1, b: 2}

10. For Loop - Reverse a String

Question: Write a for loop to reverse the string “JavaScript”.

Hint: Use the split and reverse methods to reverse a string.

Required Output: tpircSavaJ

11. For…of Loop - Square and Sum

Question: Use a for...of loop to square each number in [1, 2, 3, 4] and sum them.

Hint: Use the ** operator to square numbers.

Required Output: 30

12. For…in Loop - Count Object Properties

Question: Use a for...in loop to count how many properties are in the object {name: "Alice", age: 25, country: "USA"}.

Hint: Use the Object.keys function to count properties.

Required Output: 3

13. For Loop - List Prime Numbers

Question: Write a for loop to list all prime numbers between 1 and 30.

Required Output: 2 3 5 7 11 13 17 19 23 29

14. For…of Loop - Aggregate Strings

Question: Use a for...of loop to aggregate strings in an array into a single sentence: ["Hello", "this", "is", "an", "array"].

Hint: Use the join method to aggregate strings.

Required Output: Hello this is an array

15. For…in Loop - Print Keys and Values

Question: Use a for...in loop to print all keys and values in the object {name: "Alice", age: 25, country: "USA"}.

Hint: Use the Object.keys and Object.values functions to print keys and values.

Required Output: name Alice age 25 country USA

16. For Loop - Count Odd Numbers in Array

Question: Write a for loop to count how many odd numbers are in the array [10, 21, 33, 42, 55, 66, 77].

Hint: Use the modulo operator % to check if a number is odd or even.

Required Output: 4

17. For…of Loop - Filter Strings

Question: Use a for...of loop to filter out non-string types from the array [1, "hello", 2, "world", 3, "yes"] and print the strings.

Hint: Use the typeof function to check if a value is a string.

Required Output: hello world yes

18. For…in Loop - Modify Object

Question: Use a for...in loop to increment each value in the object {a: 1, b: 2, c: 3} by 1 and print the modified object.

Hint: Use the ++ operator to increment a number.

Required Output: {a: 2, b: 3, c: 4}

19. For Loop - Create Multiplication Table

Question: Write a for loop to create a string representing the multiplication table for the number 3, from 1 to 5.

Hint: Use the + operator to concatenate strings.

Required Output: "3x1=3 3x2=6 3x3=9 3x4=12 3x5=15"

20. For…of Loop - Double and Filter

Question: Use a for...of loop to double each number in the array [5, 8, 10, 20] and print only those results that are greater than 15.

Required Output: 20 40

By practicing these exercises, you can improve your understanding of for, for...of, and for...in loops. You can also use JavaScript arrays and objects to manipulate data efficiently.

updatedupdated2024-04-162024-04-16
comments powered by Disqus