Thursday, July 20, 2023

ARRAY REDUCE

 

ARRAY REDUCE

<!DOCTYPE html>

<html lang="en">

<head>

    <meta charset="UTF-8">

    <meta http-equiv="X-UA-Compatible" content="IE=edge">

    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <title>Document</title>

 

    <h2 style="background-color: rgb(9, 238, 9);color:rgb(255, 0, 0);font-weight: bolder;font-family: Georgia, 'Times New Roman', Times, serif;">Array Reduce</h2>

    <h5 style="color: rgb(231, 15, 166);">Output</h5>

         

</head>

<body>

    <script>

      var arr=[2,3,1,5];  

var a=arr.reduce(function (accumulator,currentValue) {  

    return accumulator+currentValue;  

    },0);  

document.write("The sum of the array elements is: "+a);  

        </script>  

    </body>  

</html>

 

Output



 



<!DOCTYPE html>

<html lang="en">

<head>

    <meta charset="UTF-8">

    <meta http-equiv="X-UA-Compatible" content="IE=edge">

    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <title>Document</title>

 

    <h2 style="background-color: rgb(9, 238, 9);color:rgb(255, 0, 0);font-weight: bolder;font-family: Georgia, 'Times New Roman', Times, serif;">Array Reduce</h2>

    <h5 style="color: rgb(231, 15, 166);">Output</h5>

         

</head>

<body>

    <script>

    var a=[30,15,5,3];  

document.write("The difference of the array elements is: ");  

document.write(a.reduce(reducer_func));  

function reducer_func(net,n){  

return net-n;  

}  

        </script>  

    </body>  

</html>

 

Output







Sum of digit using Round Function

<!DOCTYPE html>

<html lang="en">

<head>

    <meta charset="UTF-8">

    <meta http-equiv="X-UA-Compatible" content="IE=edge">

    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <title>Document</title>

 

    <h2 style="background-color: rgb(9, 238, 9);color:rgb(255, 0, 0);font-weight: bolder;font-family: Georgia, 'Times New Roman', Times, serif;">Array Reduce</h2>

    <h5 style="color: rgb(231, 15, 166);">Output</h5>

         

</head>

<body>

    <p>sum of the rounded numbers</p>

 

<p id="demo"></p>

    <script>

   const numbers = [8.5, 4.3, 3.1, 2.7,15.2];

 

document.getElementById("demo").innerHTML = numbers.reduce(getSum, 0);

 

function getSum(total, num) {

  return total + Math.round(num);

}

        </script>  

    </body>  

</html>

 

Output



0 comments:

Post a Comment