Skip to content

lab1 ‐ JavaScript Exercises

fbfreitas edited this page Sep 16, 2026 · 3 revisions

Lab 01 - JavaScript Exercises

Implement the following functions:

1. function showProperties(obj)

Receives an object obj.

Prints the name and type of each property in the object to the console.

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

NOTES:

  • To implement this function, you must use the for-in loop and indexed property access.

2. function executeFunctions(funcArray)

Receives an array funcArray.

Executes all functions present in the array, assuming that they do not receive any parameters.

Example:

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

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

executeFunctions([sayHi, "Oops", , sayBye])

// Output:
// Hi
// Bye

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}
// ]

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']

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

6. function filter(array, predicate)

Receives an array array and a function predicate.

The predicate function receives an element of the array as an argument and returns true or false, depending on whether the element should be included in the new array.

Example:

const numbers = [1, 2, 3, 4, 5]

const even = filter(numbers, n => n % 2 === 0)

// even: [2, 4]

7. function map(array, transformation)

Receives an array array and a function transformation.

The transformation function receives an element of the array as an argument and returns a new value, which will be placed at the corresponding position in the new array.

Example:

const numbers = [1, 2, 3]

const doubled = map(numbers, n => n * 2)

// doubled: [2, 4, 6]

8. function reduce(array, operation, [initialValue])

Receives an array array, a function operation, and, optionally, an initial value initialValue.

The operation function receives two arguments: the accumulator (accumulator) and the current array element (element).

On each iteration, it returns the next value of the accumulator. The final result is the accumulated value.

Example:

const numbers = [1, 2, 3, 4]

const sum = reduce(numbers, (acc, n) => acc + n, 0)

// sum: 10

9. 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}}

10. 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, the usage of any cycle instruction (for/while) or the Array.forEach method reduces the grade by 50%.

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}, 
   { }
 ]
*/

11. Console Customization

Override console.log

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!

Clone this wiki locally