Please login to start chat

ICT Solutions 21 Jun 2022, 10:22:01
Category : JavaScript Tutorials

Reduce() (JS Array Iteration)

<!DOCTYPE html>
<html>
<head>
<title>JavaScript Code Testing</title>
</head>
<body>
<p id = "demo"></p>

<button type = "button" onclick = "jsReduce()">Total</button>&nbsp;&nbsp;
<button type = "button" onclick = "jsReset()">Reset</button>

<script>
var numbers = [302, 428, 49, 92, 84, 70, 80, 10, 3, 200];
var num = numbers.sort(function(a, b){return a - b;}).join("<br/>");
document.getElementById("demo").innerHTML = num;

function jsReset(){
document.getElementById("demo").innerHTML = num;
}

function jsReduce(){
var number2 = numbers.reduce(funReduce);
document.getElementById("demo").innerHTML = number2;
}

function funReduce(total, value){
return total + value;
}


</script>
</body>
</html>

Comments:

No comment to show.

Available Services
Download Android Application