2. Longest Lines

Write a program to read a multiple line text file and write the 'N' longest lines to stdout. Where the file to be read is specified on the command line.

by Donal Devine

HTML

<div id="divConsole"></div>

CSS

.console-output {
    font-family: monospace;
    margin: 1px;
}

JavaScript

console = {
    log: function (s) {
        divConsole.innerHTML +=
            "<p class=\"console-output\">" + s + "</p>";
    }
};

var l=0;
var m=0;
var a=[];
var al=[];
var t=[];

var bar = function(s){
    a.push(s);
    al.push(s.length);
};

var foobar = function(){
    al.sort();
             
    var y = (al.length - 1) - m;
    
    var minLength = al[y];
   
    var c = 0;
    for (var i = 0; i < a.length; i++){        
        if(a[i].length >= minLength) {            
            if (c < m) t.push(a[i]);
            c++;
        }
    }
    
    for (var x = 0; x < t.length; x++){
        console.log(t[x]);
    }
};


var foo = function (s) {
    l++;
   
    switch(l){
        case 1:
            m = parseInt(s, 10);            
            break;
        case 8:            
            bar(s);            
            foobar();
            break;
        default:
            bar(s);
            break;            
    }        
};

foo("2");
foo("a");
foo("ab");
foo("def");
foo("ghi");
foo("jklmw");
foo("nopere");
foo("asdfete");