JavaScript Help So in my library file I coded an object lite
JavaScript Help
So in my library file, I coded an object literal named coins that has a cents property and two methods. I\'ve done this step which is:
\"use strict\";
var coins = {
cents: 0;
isValid: function(){
if (isNaN(this.cents) || this.cents < 0 || this.cents > 99) {
alert(\"Please enter a valid number between 0 and 99\");
return false;
}
else {
return true;
}
},
getNumber: function (divisor) {
var coins = this.cents / divisor;
if (this.cents > 0) {
this.cents = this.cents % divisor
}
return Math.floor(coins);
}
};
Now based on the code below I need to change it to the object literal to get the cents entered by the user, validate the user’s entry, and calculate the number of coins. WHat do do I for this?
\"use strict\";
var $ = function (id) { return document.getElementById(id); };
var calculateChange = function() {
var cents, quarters, dimes, nickels, pennies;
// get the number of cents from the user
cents = Math.floor(parseInt($(\"cents\").value));
if (isNaN(cents) || cents < 0 || cents > 99) {
alert(\"Please enter a valid number between 0 and 99\");
} else {
// calculate the number of quarters
quarters = cents / 25; // get number of quarters
quarters = Math.floor(quarters);
cents = cents % 25; // assign the remainder to the cents variable
// calculate the number of dimes
dimes = cents / 10; // get number of dimes
dimes = Math.floor(dimes);
cents = cents % 10; // assign the remainder to the cents variable
// calculate the number of nickels
nickels = cents / 5;
nickels = Math.floor(nickels);
// calculate the number of nickels and pennies
pennies = cents % 5;
// display the results of the calculations
$(\"quarters\").value = quarters;
$(\"dimes\").value = dimes;
$(\"nickels\").value = nickels;
$(\"pennies\").value = pennies;
}
};
window.onload = function () {
$(\"calculate\").onclick = calculateChange;
$(\"cents\").focus();
};
Solution
Your given validation function is working fine and error free..... Only thing is you are checking for cents less than zero, but check should be there for cents equal to zero also. So I am including this and marking as bold.
\"use strict\";
var $ = function (id) { return document.getElementById(id); };
var calculateChange = function() {
var cents, quarters, dimes, nickels, pennies;
// get the number of cents from the user
cents = Math.floor(parseInt($(\"cents\").value));
if (isNaN(cents) || cents <= 0 || cents > 99) {
alert(\"Please enter a valid number between 1 and 99\");
} else {
// calculate the number of quarters
quarters = cents / 25; // get number of quarters
quarters = Math.floor(quarters);
cents = cents % 25; // assign the remainder to the cents variable
// calculate the number of dimes
dimes = cents / 10; // get number of dimes
dimes = Math.floor(dimes);
cents = cents % 10; // assign the remainder to the cents variable
// calculate the number of nickels
nickels = cents / 5;
nickels = Math.floor(nickels);
// calculate the number of nickels and pennies
pennies = cents % 5;
// display the results of the calculations
$(\"quarters\").value = quarters;
$(\"dimes\").value = dimes;
$(\"nickels\").value = nickels;
$(\"pennies\").value = pennies;
}
};
window.onload = function () {
$(\"calculate\").onclick = calculateChange;
$(\"cents\").focus();
};

