Click here to Skip to main content
15,886,422 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have been asked to write a code in javascript to show the results in the output boxes based on the input data.
I have written down the following code. However regardless of which radio buttons (Simple Interest or Compound Interest) I select, the program always calculates compound interest and displays the results accordingly. Where am I going wrong, could someone please advise. Thanks in advance.

What I have tried:

HTML
function calc() {

		var p = document.getElementById("p").value;

		var r = document.getElementById("r").value;

		var t = document.getElementById("t").value;

		var int = document.getElementById.value;

		

		if (int == "si") {

		var sip = (p \* r \* t) / 100

		var ta = p + sip

		document.getElementById("i").value = sip;

		document.getElementById("a").value = ta;

		} else	{

		var cta = p\*(Math.pow((1+r/100),t))

		var cmp = cta-p

		document.getElementById("i").value = cmp;

		document.getElementById("a").value = cta;			

		}

		}








	

	<h1> Interest Calculator </h1>

	Principal = 

	<br><br><br>

	Rate of Interest = 

	<br><br><br>

	Time (in years) = 

	<br><br>

	<h1> Interest Type </h1>

	 Simple Interest

	\  \  \  

	 Compound Interest

	<br><br>

	<hr noshade="">

	Interest 

	<br><br>

	Amount 

	<br><br>

	

	\ \ \ 
Posted
Updated 28-Apr-19 21:24pm
v2

JavaScript
var int = document.getElementById.value;

Something is missing on this line, between Id and .value.
 
Share this answer
 
Resolved - see the revised code below
<!DOCTYPE html>
<html>
<head>
	<script>
		function calc() {
			p = document.getElementById("p").value;
			r = document.getElementById("r").value;
			t = document.getElementById("t").value;
			var int = document.getElementsByName("int").value;
			
			if (document.f.int[0].checked == true) {
			sip = (p * r * t) / 100;
			ta = (p*1 + sip*1);
			document.getElementById("i").value = sip;
			document.getElementById("a").value = ta;
			}
			
			else if (document.f.int[1].checked == true) {
			cta = p*(Math.pow((1+r/100),t));
			cmp = (cta*1-p*1);
			document.getElementById("i").value = cmp;
			document.getElementById("a").value = cta;			
			}
			}
	
	</script>
	
</head>
	<body>
		<form name = "f">
		<h1> Interest Calculator </h1>
		Principal = <input type = "text" id = "p" autofocus>
		<br><br><br>
		Rate of Interest = <input type = "text" id = "r">
		<br><br><br>
		Time (in years) = <input type = "text" id = "t">
		<br><br>
		<h1> Interest Type </h1>
		<input type = "radio" name = "int" value = "si"> Simple Interest
		      
		<input type = "radio" name = "int" value = "ci"> Compound Interest
		<br><br>
		<hr noshade>
		Interest <input type = "text" id = "i">
		<br><br>
		Amount <input type = "text" id = "a">
		<br><br>
		<input type = "button" name = "cal" value = "Calculate" onclick = "calc()">
		   
		<input type = "reset" value = "Reset">
		</form>	
	</body>
	
</html>
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900