JSFiddle - React, Tailwind, and code Playground

by puuga

HTML

<body>
    <h1>Mini Test 1</h1>
    <div>
        1: <input id="list1" value="1,4,7"/><br/>
        2: <input id="list2" value="6,2"/><br/>
        3: <input id="list3" value="5,2,3,8"/><br/>
        <button onclick="goPressed()">Go</button>
    </div>
   <hr/>
    <h2>Tasks</h2>
   <hr/>
    <div>A) Which list has the most elements? (e.g. 1, 2 or 3)<br/>
        Answer: <span id="taskA"></span>
    </div>
    <hr/>
    <div>B) What is the sum of the elements in all 3 lists?<br/>
        Answer: <span id="taskB"></span>
    </div>
    <hr/>
    <div>C) Which list has the largest number?<br/>
        Answer: <span id="taskC"></span>
    </div>
    <hr/>
    <div>D) Put all the elements in one list, then sort them. What is the result?<br/>
        Answer: <span id="taskD"></span>
    </div>
    <hr/>
</body>

CSS

body {
    margin: 10px;
    font-family: Helvetica;
}
h1 {
    font-size: 140%;
    margin-bottom: 20px;
}
h2 {
    font-size: 110%;
    margin: 10px 0;
}
button, hr {
    margin: 10px 0;
}

JavaScript

// test 21/7/2012

function goPressed() {

    // Input
    var list1 = $('#list1').val().split(',');
    var list2 = $('#list2').val().split(',');
    var list3 = $('#list3').val().split(',');

    // Start here
    // A
    var listA = [list1.length, list2.length, list3.length];
    var resultA = [];
    if (listA[0] == findMax(listA)) resultA.push("1");
    if (listA[1] == findMax(listA)) resultA.push("2");
    if (listA[2] == findMax(listA)) resultA.push("3");

    // B
    var listTempB = list1.concat(list2, list3);
    var resultB = sumList(listTempB);

    // C
    var maxC = findMax([findMax(list1), findMax(list2), findMax(list3)]);
    var maxC = findMaxRecursive(list1.concat(list2, list3));
    var resultC = [];
    if (maxC == findMax(list1)) resultC.push("1");
    if (maxC == findMax(list2)) resultC.push("2");
    if (maxC == findMax(list3)) resultC.push("3");

    // D
    var resultD = list1.concat(list2, list3);
    bubbleSort(resultD);

    // Output
    $('#taskA').html(resultA.toString());
    $('#taskB').html(resultB);
    $('#taskC').html(resultC.toString());
    $('#taskD').html(resultD.toString());

}

function findMax(list) {
    var max = 0;
    for (i = 0; i < list.length; i++) {
        if (parseInt(list[i]) > max) {
            max = parseInt(list[i]);
        }
    }
    return max;
}

function findMaxRecursive(list) {
    if (list.length == 1) {
        return list.pop();
    }
    if (list[0] > list[list.length - 1]) {
        list.pop();
    }
    else {
        list.shift();
    }
    return findMaxRecursive(list);
}

function sumList(list) {
    return list.length == 1 ? parseInt(list.shift()) : parseInt(list.shift()) + sumList(list);
}

function bubbleSort(list) {
    for (i = 1; i < list.length; i++) {
        for (j = 0; j < list.length - i; j++) {
            if (parseInt(list[j]) > parseInt(list[j + 1])) {
                var temp = list[j];
                list[j] = list[j + 1];
                list[j + 1] = temp;
          ...