-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconsonantValue.js
More file actions
115 lines (101 loc) · 2.76 KB
/
consonantValue.js
File metadata and controls
115 lines (101 loc) · 2.76 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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
function solve(s) {
// get the value of each letter
let abc = {
a: 1,
b: 2,
c: 3,
d: 4,
e: 5,
f: 6,
g: 7,
h: 8,
i: 9,
j: 10,
k: 11,
l: 12,
m: 13,
n: 14,
o: 15,
p: 16,
q: 17,
r: 18,
s: 19,
t: 20,
u: 21,
v: 22,
w: 23,
x: 24,
y: 25,
z: 26,
};
// start the counter with 0 value
let count = 0;
// initialize the number holder
let numArr = [];
// replace each letter that we 'dont' want with a - and then split them into arr by -
let arr = s.replace(/[aeiou]/gi, "-").split("-");
// initiate a loop with an inside loop to go through each letter of each group and star to get the values compare w the abc
for (let i = 0; i < arr.length; i++) {
for (let j = 0; j < arr[i].length; j++) {
// add each value to counter
count += abc[arr[i][j]];
}
// push each result to number holder
numArr.push(count);
// re-start counter value
count = 0;
}
// get the biggest number in number holder array
let getMax = Math.max(...numArr);
return getMax;
}
console.log(solve("zodiac"));
console.log(solve("chruschtschov"));
// a better aproach with not loosing values get the same values
// instead of using replace using match and negating values /[^aeiou]+/gi
// function solve(s) {
// let abc = {
// a:1, b:2, c:3, d:4, e:5, f:6, g:7,
// h:8, i:9, j:10, k:11, l:12, m:13,
// n:14, o:15, p:16, q:17, r:18, s:19,
// t:20, u:21, v:22, w:23, x:24, y:25,
// z:26
// }
// let count = 0
// let numArr = []
// let arr = s.match(/[^aeiou]+/gi)
// for(let i = 0;i<arr.length;i++){
// for(let j = 0; j <arr[i].length; j++){
// count += abc[arr[i][j]]
// }
// numArr.push(count)
// count=0
// }
// let getMax = Math.max(...numArr)
// return getMax
// };
// solve("chruschtschov")
// other approach
// const solve = s => {
// let ab = ['_','a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'],
// subs = s.split(/[aeiou]/g);
// let values = subs.map(el => el.split('').reduce((acc, val) => acc + ab.indexOf(val), 0));
// return Math.max(...values);
// }
// not optimal too many .map
// function solve(s) {
// let abc = {
// a:1, b:2, c:3, d:4, e:5, f:6, g:7,
// h:8, i:9, j:10, k:11, l:12, m:13,
// n:14, o:15, p:16, q:17, r:18, s:19,
// t:20, u:21, v:22, w:23, x:24, y:25,
// z:26
// }
// let count = 0
// let numArr = []
// let arr = s.match(/[^aeiou]+/ig)
// arr = arr.map(e => e.split('')).map(e => e.map(e => abc[e])).map(e => e.reduce((a,c) => a+c,0))
// let getMax = Math.max(...arr)
// return getMax
// };
// solve("chruschtschov")