forked from vsb-vaj/template-lab-2024s-01
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtask-object.js
92 lines (79 loc) · 3.19 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
// 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) => {
let total_volume = obj.width * obj.length * obj.height;
console.log(total_volume);
};
console.log("volumeOfBox");
volumeOfBox({width : 2, length : 5, height : 1});
// 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 new_obj = {firstname : "", lastname : "", age : 0, yearOfBirth : 2021};
new_obj.firstname = firstname;
new_obj.lastname = lastname;
new_obj.age = age;
new_obj.yearOfBirth -= age;
console.log(new_obj);
};
console.log("personObject");
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) => {
let sum = 0;
for(let i = 0; i < persons.length; i++)
{
sum += persons[i].budget;
}
console.log(sum);
};
console.log("getBudgets");
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) => {
let tmp;
for(let i = 0; i < vehicles.length; i++)
{
for(let j = 0; j < vehicles.length - 1; j++)
{
if(vehicles[j].price > vehicles[j+1].price)
{
tmp = vehicles[j];
vehicles[j] = vehicles[j + 1];
vehicles[j + 1] = tmp;
}
}
}
console.log(vehicles);
};
console.log("sortVehiclesByPrice");
const vehicles = [{name: "Executor Star Dreadnought", price: 999}, {name: "T-47 Airspeeder", price: 5}, {name: "AT-AT", price : 20}]
//const vehicles = [{name: "Executor Star Dreadnought", price: 5}, {name: "T-47 Airspeeder", price: 999}, {name: "AT-AT", price : 20}, {name: "TEST", price: 15}]
sortVehiclesByPrice(vehicles);