-
Notifications
You must be signed in to change notification settings - Fork 0
/
Dehaat
72 lines (56 loc) · 1.43 KB
/
Dehaat
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
const a = [['a', 2], ['a', 3], ['c', 4]];
const b = [['a', 4], ['a', 4], ['a', 4], ['c', 3], ['c', 4]];
// map = {
// 'a': [4],
// 'c': [3,4]
// }
map1 = {
'a': [2],
'b': [3],
'c': [4]
}
map2 = {
'a': [4],
'c': [3,4]
}
// 2*4 + 4*3 + 4*4
// 8+12+16
function findSum(arr1, arr2) {
const charMapping = {}
for (let i=0; i< arr2.length; ++i) {
const char = arr2[i][0];
if (charMapping[char] !== undefined) {
charMapping[char].push(arr2[i][1]);
} else {
charMapping[char] = [arr2[i][1]]
}
}
let sum = 0;
for (let i=0; i<arr1.length; ++i) {
const char = arr1[i][0];
if (charMapping[char] !== undefined) {
const arr3 = charMapping[char];
for (let j=0; j<arr3.length; ++j) {
sum += arr1[i][1]*arr3[j];
}
}
}
return sum;
}
console.log(findSum(a,b))
// const demoPromiseSuccess2secs = new Promis((res, rej) => {
// setTimeout(() => { res("done") }, 2000)
// })
// const demoPromiseFail2secs = new Promis((res, rej) => {
// setTimeout(() => { rej("failed") }, 2000)
// })
// async function demo() {
// demoPromiseSuccess2secs
// .then(data => { console.log(data)})
// .catch(err => { console.log(err)})
// try {
// const data = await demoPromiseSuccess2secs();
// } catch (err) {
// // handle err
// }
// }