Here, I want to take inputs from users. Split and store them into array. Then show the summation of array numbers.
summation experiment
<!-- for taking inputs -->
<input id="seriesInput" type="text" placeholder="please use space">
<!-- for submitting inputs -->
<input id="submiting" onclick="seriesFunction()" value="Submit" type="button">
<!-- for placing results -->
<div> Summation is <span id="sum"></span> </div>
<script type="text/javascript">
function seriesFunction()
{
value = document.getElementById("seriesInput").value;
// splitting string in an array
value_arr = value.split(" ");
alert(value_arr);
// calling reduce method for summation
var sum = value_arr.reduce(get_sum,0);
// assigning result
document.getElementById("sum").innerHTML =sum;
alert(sum);
function get_sum(total, currentVal) {
total += currentVal;
return total;
}
}
</script>