Sunday, 30 September 2018

JavaScript: Array: reduceRight: combine elements of array

It is just like reduce, instead of combinig elements from left to right, it combine elements from right to left.

Syntax
arr.reduceRight(callback[, initialValue])

callback function takes four arguments.

Argument
Description
previousValue

The value previously returned in the last invocation of the callback, or initialValue, if supplied.
currentValue

The current element being processed in the array.
currentIndex

The index of the current element being processed in the array. Starts at index 0, if an initialValue is provided, and at index 1 otherwise.
array
The array reduceRight was called upon.

initialValue
It is optional, Value to use as the first argument to the first call of the callback.

function add(x, y){
         return x+y;
}               
var sumOfEle = arr1. reduceRight(add, 0);

Above statements are used to calculate sum of elements of the array.

function mul(x, y){
         return x*y;
}
var mulOfEle = arr1.reduceRight(mul, 1);

Above statements are used to compute multiplication of elements in array.
                                                    
function max(x, y){
         return ((x>y)?x:y);
}
var maximum = arr1. reduceRight(max);

Above statements are used to compute maximum value in the array.

reduceRight.html
<!DOCTYPE html>

<html>

<head>
    <title>Array reduceRight example</title>
</head>

<body>
    <script type="text/javascript">
        var arr1 = [12, 43, 2, 3, 98, 6];

        function add(x, y) {
            return x + y;
        }

        function mul(x, y) {
            return x * y;
        }

        function max(x, y) {
            return ((x > y) ? x : y);
        }

        var sumOfEle = arr1.reduceRight(add, 0);
        var mulOfEle = arr1.reduceRight(mul, 1);
        var maximum = arr1.reduceRight(max);

        document.write("Sum of elements in array is " + sumOfEle + "<br />");
        document.write("Multiplication of elements in array is " + mulOfEle + "<br />");
        document.write("Maximum number in array is " + maximum + "<br />");
    </script>
</body>

</html>

Open above page in browser, you can able to see following text.

Sum of elements in array is 164
Multiplication of elements in array is 1820448
Maximum number in array is 98

JavaScript: Arrays: indexOf: Get the index of first occurrence of element
‘indexOf’ method is used to get the the index of first occurrence of the element. Return the index if the element is found, else -1.

Syntax
arr.indexOf(searchElement[, fromIndex = 0])

If the fromIndex value is a negative number, it is taken as the offset from the end of the array.

Argument
Description
searchElement
Element to search
fromIndex
The index to start the search at.

indexOf.html
<!DOCTYPE html>

<html>

<head>
    <title>indexOf example</title>
</head>

<body>
    <script type="text/javascript">
        var arr = [2, 3, 5, 7, 2, 3, 2, 5];

        document.write("index of first occurrence of element 2 " + arr.indexOf(2) + "<br />");

        document.write("index of occurrence of element 2 from index 5 " + arr.indexOf(2, 5) + "<br />");

        document.write("index of occurrence of element 2 from index 7 " + arr.indexOf(2, 7) + "<br />");
    </script>
</body>

</html>

Open above page in browser, you can able to see following text.

index of first occurrence of element 2 0
index of occurrence of element 2 from index 5 6
index of occurrence of element 2 from index 7 -1



Previous                                                 Next                                                 Home

No comments:

Post a Comment