Assignment 2
Assignment 2
1. Write a function that uses a `for` loop to print numbers from 1 to 10. If the number is
divisible by 3, skip printing the number. (0.5 Grade)
Output: 1, 2, 4, 5, 7, 8, 10
2. Write a function that uses a `while` loop to calculate the sum of numbers from 1 to 100.
(0.5 Grade)
3. Implement a function using `forEach` to iterate over an array and print each element. (0.5
Grade)
4. Explain the difference between `forEach` and `for...of` loops in JavaScript. (0.5 Grade)
5. Write a function that destructures an object to extract values and returns a formatted
string. (0.5 Grade)
const person = {name: 'John', age: 25 };
Output: 'John is 25 years old'
6. Use the spread operator to merge two arrays, then return the merged array. (0.5 Grade)
7. Write a function that accepts multiple parameters (two or more) and returns their sum.
(0.5 Grade)
8. Compare primitive and non-primitive data types in JavaScript with examples. (0.5 Grade)
9. Explain how hoisting works in JavaScript and describe the Temporal Dead Zone (TDZ). (1
Grade)
10. Write a function that demonstrates closure by creating a counter function that returns
the number of times it has been called. (1 Grades)
11. Create a function that returns a promise which resolves after 3 seconds with a 'Success'
message. (0.5 Grade)
12. Convert the previous promise-based function to use `async` and `await`. (0.5 Grade)
13. Create a function that returns a promise, which resolves if a random number is greater
than 5, otherwise it rejects. (1 Grade)
14. Implement a function that chains multiple .then() handlers to a promise to demonstrate
promise chaining. (1 Grade)