Question: 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

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 users 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(); };

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!