forked from vsb-vaj/template-lab-2024s-01
-
Notifications
You must be signed in to change notification settings - Fork 0
/
task-object.js
70 lines (60 loc) · 2.5 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
// 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((a, b) => a * b, 1);
};
console.log(volumeOfBox({width: 2, length: 3, height: 5}))
// 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) => {
return {
firstname: firstname,
lastname: lastname,
age: age,
yearOfBirth: new Date().getFullYear() - age,
};
};
console.log(personObject("Obi-wan", "Kenobi", "40"));
// 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((sum_, x) => sum_ + x.budget, 0);
};
console.log(getBudgets([
{name: "John", age: 21, budget: 23000},
{name: "Steve", age: 32, budget: 40000},
{name: "Martin", age: 16, budget: 2700}
]));
// 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.toSorted((a, b) => {
return a.price - b.price
});
};
const vehicles = [{name: "Executor Star Dreadnought", price: 999}, {name: "T-47 Airspeeder", price: 5}, {
name: "AT-AT",
price: 20
}]
console.log(sortVehiclesByPrice(vehicles));