Repository navigation
Expand file tree
/
Copy pathsolutions.js
More file actions
100 lines (92 loc) · 2.41 KB
/
Copy pathsolutions.js
File metadata and controls
100 lines (92 loc) · 2.41 KB
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
93
94
95
96
97
98
99
100
const sum = (...args) => {
const sum = args.reduce((acc, item) => {
return acc + item;
}, 0)
return sum;
}
const vowelize = (arr) => {
const regex = /[AEIOUaeiou]/
const vowelArr = arr.map(word => {
return word.split('').filter(letter => letter.match(regex)).join('');
})
const result = vowelArr.filter(word => word !== '');
return result;
}
const combineAndSort = (...args) => {
const flatArr = [...args].flat().sort();
return flatArr;
}
const anagramTester = (a, b) => {
const wordA = a.toLowerCase().split(' ').join('');
const wordB = b.toLowerCase().split(' ').join('');
const objA = {};
const objB = {};
for(const letter of wordA) {
objA[letter] ? objA[letter] += 1 : objA[letter] = 1;
}
for(const letter of wordB) {
objB[letter] ? objB[letter] += 1 : objB[letter] = 1;
}
const keysA = Object.keys(objA)
const keysB = Object.keys(objB)
if(keysA.length !== keysB.length) return false;
for(let key of keysA){
if(objA[key] !== objB[key]) return false;
}
return true;
}
const objectForEach = (obj, callback) => {
for(const key in obj){
callback(obj[key]);
}
}
const updateAtPath = (obj, string, value) => {
const keyArr = string.split('.');
const firstKeys = keyArr.slice(0, keyArr.length - 1);
const lastKey = keyArr.slice(keyArr.length - 1);
const resultPath = firstKeys.reduce((acc, key) => {
return acc[key];
}, obj)
resultPath[lastKey] = value;
}
class Car {
constructor({initialFuelLevel, fuelCapacity}) {
this.fuelLevel = initialFuelLevel;
this.fuelCap = fuelCapacity;
}
getFuelLevel = () => {
return this.fuelLevel;
}
addFuel = (fuel) => {
this.fuelLevel += fuel;
if(this.fuelLevel > this.fuelCap) this.fuelLevel = this.fuelCap;
}
}
const whatWouldYouLikeToCheckOut = async (libraryApi) => {
const books = [];
const authors = await libraryApi['favoriteAuthors']();
const results = await Promise.all(authors.map(author => libraryApi['booksAvailableBy'](author)));
results.flat().forEach(book => {
if(book.author) books.push(book.title);
})
return books;
}
const calc = (left) => {
return (operator) => {
return (right) => {
return eval(`${left}${operator}${right}`);
}
}
}
module.exports = {
exampleFunction: function(message) { return message; },
sum,
vowelize,
combineAndSort,
anagramTester,
objectForEach,
updateAtPath,
Car,
whatWouldYouLikeToCheckOut,
calc
}