-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharrays-objects.js
50 lines (45 loc) · 1.31 KB
/
arrays-objects.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
function findArithmeticMeanFirstDecision(array) {
let amount = 1;
return array.reduce((accumulator, currentValue, index) =>
accumulator + ((currentValue % 2 === 1 && index % 2 === 0) ? (amount++, currentValue) : 0)
) / amount;
}
function findArithmeticMeanSecondDecision(array) {
let sum = 0;
let amount = 0;
array.forEach((element, index) => {
if (element % 2 === 1 && index % 2 === 0) {
sum += element;
amount++;
}
})
return sum / amount;
}
function getTheAmountOfTheCheck(array) {
return array.reduce((accumulator, currentValue) => {
let {amount, price} = currentValue;
return accumulator + amount * price;
}, 0);
}
function getACheckObject(array) {
return array.reduce((accumulator, currentValue) => {
let [name, amount, price] = currentValue;
accumulator.push({name: name, amount: amount, price: price});
return accumulator;
}, []);
}
function filterObject(obj) {
let result = {};
for (let prop in obj) {
if (prop.includes("a"))
result[prop] = obj[prop];
}
return result;
}
function filterObjectSecondDecision(obj) {
let result = {};
Object.keys(obj).forEach(key => {
if (key.includes("a")) result[key] = obj[key];
});
return result;
}