cploutarchou
2/5/2020 - 3:30 PM

Create Custom For Each Function for Node List

Create Custom For Each Function for Node List

forEach =  function (percentages) {
  //1st we select fields we want add new values
     var fields = document.querySelectorAll(DOMStrings.expensesPercentageLabel);
  
  //The we pass a function on var eg nodeListForeach
  // we pass the fields list and we retrunt a callback function on secont parameter
  // we loop inside of fields and we pass on callback function the field index no and second
  // the current field value.
     var nodeListForeach = function (fields, callback) {
         for (var i = 0; i < fields.length; i++) {
             callback(fields[i], i);
         }
     };
  //the we call  nodeListForeach function constructor en pass the fields array end 
  // get the index id and index value from the callback function
     nodeListForeach(fields, function (currentField, indexValue) {
         if (percentages[indexValue] > 0) {
             currentField.textContent = percentages[indexValue] + '%';
         } else {
             currentField.textContent = "---";
         }
     })
 }