forked from vsb-vaj/template-lab-2024s-01
-
Notifications
You must be signed in to change notification settings - Fork 0
/
task-object.js
55 lines (47 loc) · 2.02 KB
/
task-object.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
// Checkout useful functions at: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object
// Checkout useful functions at: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array
// 1 ----
// Create a function that takes an object argument sizes (contains width, length, height keys) and returns the volume of the box.
// Examples
// volumeOfBox({ width: 2, length: 5, height: 1 }) ➞ 10
// volumeOfBox({ width: 4, length: 2, height: 2 }) ➞ 16
// volumeOfBox({ width: 2, length: 3, height: 5 }) ➞ 30
// Your code:
const volumeOfBox = (obj) => {
return Object.values(obj).reduce((_acc, _item) => _acc * _item);
};
// 2 ----
// Create a function that takes strings - firstname, lastname, age, and return object with firstname, lastname, age, yearOfBirth
// Examples
// personObject("Obi-wan", "Kenobi", "40") ➞ { firstname: "Obi-wan", lastname: "Kenobi", age: 40, yearOfBirth: 1981 }
// Your code:
const personObject = (firstname, lastname, age) => {
const currYear = new Date().getFullYear();
return {
firstname,
lastname,
age,
yearOfBirth: currYear - age,
};
};
// 3 ----
// Create the function that takes an array with objects and returns the sum of people's budgets.
// Example
// getBudgets([
// { name: "John", age: 21, budget: 23000 },
// { name: "Steve", age: 32, budget: 40000 },
// { name: "Martin", age: 16, budget: 2700 }
// ]) ➞ 65700
//Your code:
const getBudgets = (persons) => {
return (persons.reduce((_acc, { budget }) => budget + _acc, 0));
};
// 4 ----
// Create function that takes array of cars and sort them by price
// Example
const vehicles = [{name: "Executor Star Dreadnought", price: 999}, {name: "T-47 Airspeeder", price: 5}, {name: "AT-AT", price : 20}]
// sortVehiclesByPrice(vehicles) ➞ [{name: "T-47 Airspeeder", price :5}, {name: "AT-AT", price :20}, {name: "Executor Star Dreadnought", price: 999}]
// Your code:
const sortVehiclesByPrice = (vehicles) => {
return vehicles.sort((obj1, obj2) => obj1.price - obj2.price);
};