JSFiddle - React, Tailwind, and code Playground

by Andrew Poes

HTML

<input id="files" type="file" />
<br/>
<!-- Using names.txt (right click and 'Save Link/Target As...'), a 46K text file containing over five-thousand first names, begin by sorting it into alphabetical order. Then working out the alphabetical value for each name, multiply this value by its alphabetical position in the list to obtain a name score.

For example, when the list is sorted into alphabetical order, COLIN, which is worth 3 + 15 + 12 + 9 + 14 = 53, is the 938th name in the list. So, COLIN would obtain a score of 938 Ă— 53 = 49714.

What is the total of all the name scores in the file? -->

CSS

.print {
    position: relative;
    display: inline-block;
    background-color: black;
    color: white;
    font-family: Helvetica, Helvetica-Neue, sans-serif;
    font-weight: bold;
    font-size: 24px;
    letter-spacing: -1.5px;
    padding: 4px 8px;
}

body {
    background-color: #eeeeee;
}
}

input {
    padding: 20px;
}
}

JavaScript

$(document).ready(function() {
    //Check File API support
    if (window.File && window.FileList && window.FileReader) {
        var filesInput = document.getElementById("files");
        filesInput.addEventListener("change", function(event) {
            var files = event.target.files; //FileList object
            var output = document.getElementById("result");
            for (var i = 0; i < files.length; i++) {
                var file = files[i];
                //Only plain text
                if (!file.type.match('plain')) continue;
                var fileReader = new FileReader();
                fileReader.addEventListener("load", function(event) {
                    var text = event.target;
                    var result = text.result
					var names = parseNames(result)
                });

                //Read the text file
                fileReader.readAsText(file);
            }

        });
    }
    else {
        console.log("Your browser does not support File API");
    }
})

function parseNames(str) {
    str = str.replace(/\"/g, "") // remove quotation marks from strings
    var names = str.split(",");
    names = quickSort(names, 0, names.length - 1);
    return names;
}

function quickSort(arr, start, end) {
	if (start < end) {
        var pIndex = partition(arr, start, end);
        quickSort(arr, start, pIndex - 1);
        quickSort(arr, pIndex + 1, end);
    }
    return arr;
}

function partition(arr, start, end) {
    var pIndex = start;
    var pivot = arr[end];
    for (var i = start; i < end; ++i) {
        if (arr[i] <= pivot) {
            var t = arr[pIndex];
            arr[pIndex] = arr[i];
            arr[i] = t;
            ++pIndex;
        }
    }
    var t = arr[pIndex];
    arr[pIndex] = arr[end];
    arr[end] = t;
    return pIndex;
}

function print() {
    var args = Array.prototype.slice.apply(arguments)
    var str = ""
    for (arg of args) {
        str += arg + ", "
    }
    str = str.substring(0,...