JSFiddle - React, Tailwind, and code Playground

by andrew rowe

JavaScript

function addDigitValue(previousValue, currentDigit, currentIndex, array) {
    console.log( previousValue, currentDigit, currentIndex, array);
    var exponent = (array.length - 1) - currentIndex;
    var digitValue = currentDigit * Math.pow(10, exponent);
    console.log('return',previousValue + digitValue);
    return previousValue + digitValue;
    }

function sumValue(previousValue, currentDigit, currentIndex, array) {
    console.log( 'sumValue', previousValue, currentDigit, currentIndex, array);
    return previousValue + currentDigit;
    }


var digits = [4, 1, 2, 5];

// Determine an integer that is computed from values in the array.
var result = digits.reduce(sumValue);
var ul = document.createElement('ul');
var t=document.createTextNode(result);
var li = document.createElement('li');
li.appendChild(t);
ul.appendChild(li)
document.body.appendChild(ul);
// Output: 4125