Quick Reference
The Array reduceRight() method executes a reducer function for every array item, working from right to left, returning a single value representing the function’s accumulated result.
<!-- html element to place output -->
<p id="my_total"></p>
// arrays
let my_numbers = [200, 12, 18];
let my_total = my_numbers.reduceRight(my_function);
// function to run (adds numbers in array)
function my_function(total, number) {
return total + number;
}
// output the square roots of all the array items
document.getElementById('my_total').innerHTML = 'Total: ' + my_total;
The way this works (see Syntax section below):
- The reduceRight() method cycles through the array starting from the end, doing something with the items (in this case, subtracting them)
Note
Arrays count starting from zero NOT one. So item 1 is position [0], item 2 is position [1], and item 3 is position [2] … and so on.
Output
Total: -194
Syntax
array.reduce(function(total, currentValue, currentIndex, arr), initialValue)
Parameters
Parameter | Description |
---|---|
function() | A function to be run for each element in the array (required) |
total | The initialValue, or the previously returned value of the function (required) |
currentValue | The value of the current element (required) |
currentIndex | The index of the current element |
arr | The array the current element belongs to |
initialValue | A value to be passed to the function as the initial value |
JavaScript Notes:
- When using JavaScript, single or double quotation marks are acceptable and work identically to one another; choose whichever you prefer, and stay consistent
- JavaScript is a case-sensitive language; firstName is NOT the same as firstname
- Arrays count starting from zero NOT one; so item 1 is position [0], item 2 is position [1], and item 3 is position [2] … and so on
- JavaScript variables must begin with a letter, $, or _
- JavaScript variables are case sensitive (x is not the same as X)
We’d like to acknowledge that we learned a great deal of our coding from W3Schools and TutorialsPoint, borrowing heavily from their teaching process and excellent code examples. We highly recommend both sites to deepen your experience, and further your coding journey. We’re just hitting the basics here at 1SMARTchicken.