jeudi 19 juillet 2018

Beginner JS exercise: Change Maker

I am in the process of learning JavaScript, and have hit a roadblock with an exercise. I'm trying to make a change maker app, and can't figure out how to successfully add an if/else statement. I'm hoping someone can give me some guidance. I have everything working, but when I try to add the if/else statement that restricts the input to 0-99, it breaks. I am getting an error that says cents is not defined. Is it my placement? Is the code wrong? Any help would be greatly appreciated!

See Pen Here: https://codepen.io/bradbee/pen/BPQjEv

var $ = function(id) {
    return document.getElementById(id);
};

var $ = function(id) {
    return document.getElementById(id);
};


var processEntry = function() {
    var entry = $("cents").value;         // get user entry
    var cents = parseInt(entry);          // parse entry
    makeChange(cents);
    $("cents").focus();
};


if (!isNaN(cents) || cents<100 || cents>0){
        var makeChange = function(cents) {
    var quarters = parseInt(cents / 25);  // get number of quarters

    var dimes = parseInt(((cents-(quarters*25)) / 10));                        // get number of dimes

    var nickels = parseInt (((cents-(quarters*25))-(dimes*10)) / 5);                      // get number of nickels

    var pennies = parseInt (((cents-(quarters*25)) - (dimes*10)) - (nickels*5));                      // get number of pennies


        // display the results of the calculations
    $("quarters").value = quarters;
    $("dimes").value = dimes;
    $("nickels").value = nickels;
    $("pennies").value = pennies;
};
} else{
    alert("Please enter a number between 0-99")
}

window.onload = function () {
    $("calculate").onclick = processEntry;
    $("cents").focus();
};

Thanks so much!

Aucun commentaire:

Enregistrer un commentaire