-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchallenge_2.js
More file actions
32 lines (29 loc) · 849 Bytes
/
challenge_2.js
File metadata and controls
32 lines (29 loc) · 849 Bytes
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
/*
Challenge 2; Longest Lines
The file contains multiple lines. The first line indicates the number of lines you should output, the other lines are of different length and are presented randomly.
*/
function topLongest(arStrings) {
"use strict";
var numStrings = parseInt(arStrings.shift(), 10 ), topStrings = [];
var orderedStrings = arStrings.sort(
function(a,b) {
return (b.length - a.length);
}
);
for(var i = 0; i < numStrings; i += 1) {
topStrings.push(orderedStrings[i]);
}
return topStrings;
}
var fs = require("fs");
var lines = [];
fs.readFileSync(process.argv[2]).toString().split('\n').forEach(function (line) {
"use strict";
if (line !== "") { // ignore empty lines
lines.push(line);
}
});
var x = topLongest(lines);
for(var i = 0, j = x.length; i < j; i += 1) {
console.log(x[i]);
}