我的更改计算器上的问题非常简单。 [英] Very simple question on my change calculator.

查看:76
本文介绍了我的更改计算器上的问题非常简单。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿,我只是想知道是否有人可以帮我修复我的问题。

我的代码可以正常工作,目的是计算你需要多少钱来换回来( 0-99美分)。

我想知道是否有人可以帮助我这样做,所以网页显示的最小数字为5美分,10美分,20美分和50美分,代表制作的硬币达到指定的分数。我猜你需要使用%,但我不确定我会怎么做。任何帮助将非常感谢!

Hey I was just wondering if anyone could help me with fixing my problem.
My code works fine for its current purpose which is to calculate how many coins you would need to give back for change (0-99 cents).
I was wondering if anyone could help me make it so the webpage displays the minimum number of 5 cents, ten cents, 20 cents, and 50 cents that represent the coins that make up the specified number of cents. I'm guess you need to use % but i'm not sure how I would go about this. Any help would be very appreciated!

<!DOCTYPE html>
<html>
	<head>
		<script>
			const fiveChange = 5;
			const tenChange = 10;
			const twentyChange = 20;
			const fiftyChange = 50;
			
			var $ = function(id){
		
				return document.getElementById(id);
			}
			
			
			function calculateFiftyCents(){
				var fiftyCents = parseInt($("amount").value) % fiftyChange;
				fiftyCents=parseInt(fiftyCents);
				fiftyCents=fiftyCents.toFixed(0);
				return fiftyCents;
					
			}
			function calculateTwentyCents(){
				var twentyCents = parseInt($("amount").value) % twentyChange;
				twentyCents=parseInt(twentyCents);
				twentyCents=twentyCents.toFixed(0);
				return twentyCents;
				}
							
			function calculateTenCents(){
				var tenCents = parseInt($("amount").value) % tenChange;
				tenCents=parseInt(tenCents);
				tenCents=tenCents.toFixed(0);
				return tenCents;
				}
			function calculateFiveCents(){
				var fiveCents = parseInt($("amount").value) % fiveChange;
				fiveCents=parseInt(fiveCents);
				fiveCents=fiveCents.toFixed(0);
				return fiveCents;
				}
			
			
			
			
			function processEntries(){
				var amount = parseInt($("amount").value);
			
				if (isNaN(amount) || amount>99){
					alert("Change must be numeric and under 99")
				
				}
					else{
						$("fiftyCents").value = calculateFiftyCents();
						$("fiveCents").value = calculateFiveCents();
						$("tenCents").value = calculateTenCents();
						$("twentyCents").value = calculateTwentyCents();
												
					}
				
				
			}
			
			window.onload = function(){ //process click of calculation related to the button
				$("calculate").onclick = processEntries;
				$("amount").focus(); //blinks cursor
			}
			
			
		</script>
	
	</head>
	
	<body>
		<main>
			<h1> Change Calculator </h1><br> 
			<label for="amount">Enter number of cents (0-99).</label>	
			<input type="text" id="amount">
			<label> </label> 
			<input type="button" id="calculate" value="calculate"><br>
			<p><label for="fiveCents">Five cents:  </label>
			<input type="text" id="fiveCents"><br>
			<label for="tenCents">Ten cents:  </label>
			<input type="text" id="tenCents"><br>
			<label for="twentyCents">Twenty Cents:   </label>
			<input type="text" id="twentyCents"><br>
			<label for="fiftyCents">Fifty Cents:   </label>
			<input type="text" id="fiftyCents"><br>
			
		</main>	
	</body>
	
</html>





我的尝试:



我尝试使用这样的代码但是适应了上面的代码。



What I have tried:

I have tried using code like this but adapted to the code above.

function leftOverEggs(){
var remaining = parseInt($("eggs").value) % EGGS_IN_A_CARTON;
return remaining;

推荐答案

= function (id){

return document .getElementById(id);
}


function calculateFiftyCents(){
var fiftyCents = parseInt
= function(id){ return document.getElementById(id); } function calculateFiftyCents(){ var fiftyCents = parseInt(


amount)。value)%fiftyChange;
fiftyCents = parseInt (fiftyCents);
fiftyCents = fiftyCents.toFixed( 0 );
return fiftyCents;

}
function calculateTwentyCents(){
var twentyCents = parseInt
("amount").value) % fiftyChange; fiftyCents=parseInt(fiftyCents); fiftyCents=fiftyCents.toFixed(0); return fiftyCents; } function calculateTwentyCents(){ var twentyCents = parseInt(


amount)。value)%twentyChange;
twentyCents = parseInt (twentyCents);
twentyCents = twentyCents.toFixed( 0 );
return twentyCents;
}

function calculateTenCents(){
var tenCents = parseInt
("amount").value) % twentyChange; twentyCents=parseInt(twentyCents); twentyCents=twentyCents.toFixed(0); return twentyCents; } function calculateTenCents(){ var tenCents = parseInt(


这篇关于我的更改计算器上的问题非常简单。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆