JSFiddle - React, Tailwind, and code Playground

by puuga

HTML

<html>
<body>
    <h1>loops and array</h1>
    <!--<form onsubmit="operation()">-->
    <input id="inputS" size="50" onkeyup="operation()"/>
    <!--</form>-->
    <button onclick="operation()">do average</button>
    <div id="result"></div>
</body>
</html>

CSS

body{
    font-size:150%;
}

JavaScript

function operation() {
    //string input
    //var input = document.getElementById('inputS').value;
    var input = $("#inputS").val();
    //alert(input);
    
    //convert sting to arrString 
    var arrInput = input.split(",");
    
    //init sum
    sum = 0;
    
    //do summary
    for(i=0; i<arrInput.length; i++) {
        sum += parseInt(arrInput[i]);
        //sum += 1*arrInuput[i];
    }
    
    //output
    $("#result").html("Average of "+ input +" = " + sum/arrInput.length);
    
}