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

Javascript Exercise 2

The document contains a series of JavaScript exercises designed to test various programming skills. Tasks include creating functions for filtering even numbers, returning the day of the week from a Date object, working with Map objects, and implementing classes with methods. Additional exercises involve using array methods like map, filter, and reduce to manipulate data.

Uploaded by

mugisha zila
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)
34 views2 pages

Javascript Exercise 2

The document contains a series of JavaScript exercises designed to test various programming skills. Tasks include creating functions for filtering even numbers, returning the day of the week from a Date object, working with Map objects, and implementing classes with methods. Additional exercises involve using array methods like map, filter, and reduce to manipulate data.

Uploaded by

mugisha zila
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

Javascript exercise

1.Write a function that takes an array of numbers and returns a new array containing only the
even numbers.

Example:​
Input: [1, 2, 3, 4, 5, 6]​
Output: [2, 4, 6]

2.Write a function that accepts a JavaScript Date object and returns the day of the week as a
string (e.g., "Monday").

Example:​
Input: new Date("2025-06-10")​
Output: "Tuesday"

3.Write a function that accepts a Map object and returns an array of all its keys.

Example:​
Input: new Map([["name", "John"], ["age", 30]])​
Output: ["name", "age"]

4.Create a class named Car that has a method drive() which logs "The car is driving" to the
console. Instantiate the class and call the drive() method.

5.Use an arrow function with the .map() method to convert an array of numbers into their
squares.

Example:​
Input: [2, 3, 4]​
Output: [4, 9, 16]

6.Create a class Animal with a method makeSound(). Then create a subclass Dog that calls
makeSound() using super and adds its own message like "The dog barks."

7.Write an arrow function that returns an object with keys name and age from parameters.

Example:​
Input: ("Alice", 25)​
Output: { name: "Alice", age: 25 }
8.Given an array of numbers, write a function using filter() and map() to return the squares of
only the even numbers.

Example:​
Input: [1, 2, 3, 4]​
Output: [4, 16]

9.Write a function that flattens a nested array (one level deep).

Example:​
Input: [1, [2, 3], 4, [5]]​
Output: [1, 2, 3, 4, 5]​

10.Write a function that returns an array of duplicate elements from the input array.

Example:​
Input: [1, 2, 3, 2, 4, 5, 1]​
Output: [1, 2]

11.Use reduce() to sum all numbers in an array.

Example:​
Input: [10, 20, 30]​
Output: 60

You might also like