Sweater Cost Calculator

In this example below you will see how to do a Sweater Cost Calculator with some HTML / CSS and Javascript

Thumbnail
This awesome code was written by lowbelly, you can see more from this user in the personal repository.
You can find the original code on Codepen.io
Copyright lowbelly ©
  • HTML
  • CSS
  • JavaScript
    
<h1>OMG, you could sell those sweaters! How much do they cost?</h1>
<p>Enter the number of weeks it takes to make a sweater and press return.</p>
<input type="text" id="weeks" />
<h2 id="results"></h2>

/*Downloaded from https://www.codeseek.co/lowbelly/sweater-cost-calculator-BzRpyQ */
    
body {
  background-color:lightpink;
}
img {
  width: 200px;
}


/*Downloaded from https://www.codeseek.co/lowbelly/sweater-cost-calculator-BzRpyQ */
    // enter the number of weeks

// These sweaters cost $x, how many would you like?
var workingHoursInWeeks = 56;
var minWage = 9.25;
var results = document.getElementById("results");

// weeks * working hours * minimum wage
function sweaterCostCalc(weeks){
  var sweaterCost = weeks * workingHoursInWeeks * minWage;
  return sweaterCost;
}

// add commas
function addCommas(nStr)
{
    nStr += '';
    var x = nStr.split('.');
    var x1 = x[0];
    var x2 = x.length > 1 ? '.' + x[1] : '';
    var rgx = /(\d+)(\d{3})/;
    while (rgx.test(x1)) {
        x1 = x1.replace(rgx, '$1' + ',' + '$2');
    }
    return x1 + x2;
}

document.getElementById("weeks").addEventListener("keyup", function(event){
  var userEnteredWeeks = document.getElementById("weeks").value;
  var Cost = sweaterCostCalc(userEnteredWeeks);
  var totalCost = addCommas(Cost);
  
  
  if (event.keyCode == 13){
    results.innerHTML = "These sweaters cost $" + totalCost + " each. How many would you like?";
  }
  
});




Comments