JSFiddle - React, Tailwind, and code Playground

by Hastig Z

JavaScript

var tips = [];
var bills = [124, 48, 264];
function calculateTips() {
	var forEachCounter = 0;
  bills.forEach(function(amount) {
    forEachCounter++;
    switch(true) {
      case (amount <= 50):
        tips.push(amount * 0.2);
        break;
      case (amount > 50):
        tips.push(amount * 0.5);
        break;
    }
    if (forEachCounter === bills.length) {
      var totalTips = 0;
      var forEachCounter2 = 0;
      tips.forEach(function(tipamount) {
        forEachCounter2++;
        totalTips += tipamount;
        if (forEachCounter2 === tips.length) {
          //alert(totalTips);
          // output it to where you need it
          document.getElementsByTagName('body')[0].innerHTML = totalTips;
        }
      })
    }
  })
}
// call it when you need it
calculateTips();