JSFiddle - React, Tailwind, and code Playground

by puuga

HTML

total<input id="total" type="number"></input><br/>
bill<input id="bill" type="number"></input><br/>
<button onclick="goPress()">go</button>
<div id="output"></div>

JavaScript

// change
function goPress() {
    var total = 1*document.getElementById("total").value;
    var bill = 1*document.getElementById("bill").value;
    
    var change = bill - total;
    var output = "change ="+ change;
    
    if(change >= 1000) {
        var temp = Math.floor(change/1000);
        output += "<br/>1000 bill = " + temp;
        change %= 1000;
    }
    if(change >= 500) {
        var temp = Math.floor(change/500);
        output += "<br/>500 bill = " + temp;
        change %= 500;
    }
    if(change >= 100) {
        var temp = Math.floor(change/100);
        output += "<br/>1000 bill = " + temp;
        change %= 100;
    }
    if(change >= 50) {
        var temp = Math.floor(change/50);
        output += "<br/>50 bill = " + temp;
        total %= 50;
    }
    if(change >= 20) {
        var temp = Math.floor(change/20);
        output += "<br/>20 bill = " + temp;
        total %= 20;
    }
    if(change >= 10) {
        var temp = Math.floor(change/10);
        output += "<br/>10 coin = " + temp;
        change %= 10;
    }
    if(change >= 5) {
        var temp = Math.floor(change/5);
        output += "<br/>5 coin = " + temp;
        total %= 5;
    }
    if(change >= 2) {
        var temp = Math.floor(change/2);
        output += "<br/>2 coin = " + temp;
        change %= 2;
    }
    if(change >= 1) {
        var temp = Math.floor(change/1);
        output += "<br/>1 coin = " + temp;
        change %= 1;
    }
    document.getElementById("output").innerHTML = output;
}