2021-03-31 21:56:49 -04:00

51 lines
952 B
JavaScript

"use strict"
/* Make an application that takes in a coin ammount
* From 0-99.
* return a best change amount.
* Example: 27
* 1 quarters
* 0 dimes
* 0 nickels
* 2 pennies
*/
let userTotal = 0, quarters=0, dimes=0, nickels=0;
function getUserChange() {
while (true) {
userTotal = prompt("Enter an ammount between 0-99: ");
if (isNaN(userTotal)) {
alert("Only enter numbers")
continue;
}
if (userTotal < 0){
alert("Only positive numbers!")
continue;
}
if (userTotal > 99){
alert("Your number is too hight!")
continue;
}
break;
}
return userTotal;
}
userTotal = getUserChange()
quarters = userTotal /25;
userTotal %= 25;
dimes = userTotal /10;
userTotal %= 10;
nickels = userTotal /5;
userTotal %= 5;
alert("Quarters: " + parseInt(quarters) + "\n" +
"Dimes: " + parseInt(dimes) + "\n" +
"Nickels: " + parseInt(nickels) + "\n" +
"Pennies: " + parseInt(userTotal) + "\n")