-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchallenge_83.js
More file actions
64 lines (55 loc) · 1.03 KB
/
challenge_83.js
File metadata and controls
64 lines (55 loc) · 1.03 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
function justLetters(s) {
var temp;
var temp = s.toUpperCase().split("").filter( function(e,i,a) {
return e.match(/^[A-Z]$/i);
});
return temp;
}
function createMap(arr) {
var m = {}
,j = arr.length
,i = 0
,key
;
while( i < j ) {
key = arr[i];
if ( key in m ) {
m[key] += 1;
} else {
m[key] = 1;
}
i += 1;
}
return m;
}
function Score(s) {
var map = createMap( justLetters(s) )
, vals = []
, k
, i = 0
, j = 0
, beauty = 26
, sum = 0
;
// make an array of values
Object.keys(map).forEach(function(key) {
vals.push( map[key] );
});
// sort values in numerical reverse order
vals.sort( function(a,b) {
return b - a;
});
j = vals.length;
while( i < j) {
sum += beauty * vals[i];
beauty -= 1
i += 1
}
return sum;
}
var fs = require("fs");
fs.readFileSync(process.argv[2]).toString().split('\n').forEach(function (line) {
if (line !== "") { // ignore empty lines
console.log(Score(line));
}
});