JSFiddle - React, Tailwind, and code Playground

by dkunin

HTML

<div id='result'></div>

JavaScript

var items = [{price: 10}, {price: 120}, {price: 1000}];

var reducers = {
  totalInDollar: function(state, item) {
    state.dollars += item.price;
    return state;
  },
  totalInEuros : function(state, item) {
    state.euros += item.price * 0.897424392;
    return state;
  },
  totalInPounds : function(state, item) {
    state.pounds += item.price * 0.692688671;
    return state;
  },
  totalInYen : function(state, item) {
    state.yens += item.price * 113.852;
    return state;
  }
};


var combineTotalPriceReducers = function(reducers) {
  return function(state, item) {
    return Object.keys(reducers).reduce(
      function(nextState, key) {
        reducers[key](state, item);
        return state;
      },
      {}      
    );
  }
};

var bigTotalPriceReducer = combineTotalPriceReducers(reducers);

var initialState = {dollars: 0, euros:0, yens: 0, pounds: 0};

var totals = items.reduce(bigTotalPriceReducer, initialState);

var node = document.createElement("p");               
var textnode = document.createTextNode(JSON.stringify(totals));       
node.appendChild(textnode);  

document.getElementById('result').appendChild(node);

console.log(totals);