Skip to content
fbfreitas edited this page Nov 16, 2025 · 4 revisions

JavaScript Exercises

1. Working with Objects and Arrays, implement the following functions:

1.1 function showProperties(obj)

Receives an object obj. Shows in the console the name and type of each property in the object.

Example:

const o = {a: 1, b: 'Thor', c: [1,2,3], d: {x: 10}}
showProperties(o)
// Output:
// a: number
// b: string
// c: object
// d: object

1.2 function executeFunctions(funcArray)

Receives an array funcArray. Executes all functions present in the array, assuming they do not receive parameters.

Example:

function sayHi() { console.log('Hi') }
function sayBye() { console.log('Bye') }

executeFunctions([sayHi, sayBye])
// Output:
// Hi
// Bye

1.3 function filterProduct(products, minPrice)

Receives an array products (objects with at least a price property) and a number minPrice. Returns a new array containing only the products whose price is greater than minPrice.

Example:

const products = [
  {name: 'Apple', price: 5},
  {name: 'Orange', price: 10},
  {name: 'Banana', price: 3}
]

const expensive = filterProduct(products, 4)
// expensive: [
//   {name: 'Apple', price: 5},
//   {name: 'Orange', price: 10}
// ]

1.4 function mapProduct(products)

Receives an array products. Returns a new array containing only the names of the products.

Example:

const products = [
  {name: 'Apple', price: 5},
  {name: 'Orange', price: 10},
  {name: 'Banana', price: 3}
]

const names = mapProduct(products)
// names: ['Apple', 'Orange', 'Banana']

1.5 function reduceProduct(products)

Receives an array products. Returns the total sum of the prices of all products.

Example:

const products = [
  {name: 'Apple', price: 5},
  {name: 'Orange', price: 10},
  {name: 'Banana', price: 3}
]

const total = reduceProduct(products)
// total: 18

1.6 function filterProperties(propNames,obj)

Receives a string array in propNames and an object in obj.
Returns a new object with the properties from obj whose names are present in propNames.
If propNames contains names that do not exist in obj, those properties are not added to the returned object.

Example:

const o = {a: 1, b: 'Thor', c: [1,2,3], d: {x: 10}, e: 2, f: 'Captain America'}

const props = ['b', 'd', 'g', 'a']
const oFiltered = filterProperties(props, o)
// oFiltered: {a: 1, b: 'Thor', d: {x: 10}}

1.7 function filterPropertiesN(propNames,objs)

Receives a string array in propNames and an object array in objs.
Returns a new object array with objects produced by applying the filterProperties function with propNames to each object in objs.

NOTE: In this implementation, you should avoid the usage of any cycle instruction (for/while) or the Array.forEach.

Example:

const objs = [
   {a: 1, b: 'Thor', c: [1,2,3], d: {x: 10}, e: 2, f: 'Captain America'},
   {b: 'Hulk', a: [1,2,3], d: {x: 10}, e: 2, g: false}, 
   {x: 'Vision', y: false}
]

const props = ['b', 'd', 'g', 'a']
const objsFiltered = filterPropertiesN(props, objs)
/*
 objsFiltered: [
   {a: 1, b: 'Thor', d: {x: 10}},
   {b: 'Hulk', a: [1,2,3], d: {x: 10}, g: false}, 
   { }
 ]
*/

2. Modularity and Testing

2.1 ECMAScript Module

Include the two functions from Exercise 1.6 and 1.7 in an ECMAScript module so that they can be imported and reused.
Create a separate module containing example code that imports this module and demonstrates how to call the functions.

2.2 Unit Tests with Mocha

Write a set of unit tests for the function in Exercise 1.7 using the Mocha testing framework.
The tests should verify that the function behaves correctly with various inputs, including edge cases.


3. Console Customization

Write a JavaScript function that overrides the default console.log behavior so that every message logged to the console is automatically prefixed with the current date and time.

Note: You can use Date() to get a string with the current date and time in the default format.

Example:

console.log("Hello, world!");

Output:

Mon Sep 15 2025 10:42:30 GMT+0000 (Coordinated Universal Time) - Hello, world!