Click here to Skip to main content
15,896,118 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello. I am new to Javascript and I want to make a credit hour calculator. I have it almost complete, but I want it to calculate the total number of credit hours: for example if I put in three 3 credit hours that is 9 credit hours total. I know this might sound confusing but right now all it does is add up the total number. Right now there are three text fields called 1 credit hour, 2 credit hours, and 3 credit hours. For example if someone were to put in that they were taking 1 1credit hour course, 2 2credit hour course and 3 3credit hour courses I want the total to come out to be 14 credit hours. Here is my JS Fiddle file for it so you can make more sense of it: JS Fiddle

Also Here is my code I have so far:

JavaScript
function doCalc() 
{
var value1 = document.getElementById("credit1").value;
var value2 = document.getElementById("credit2").value;
var value3 = document.getElementById("credit3").value;
var total = Number(value1) + Number(value2) + Number(value3);
document.getElementById("output").value = total;
}


HTML
1 credit hour: <input type="number" id="credit1">
<br /> 

2 credit hour: <input type="number" id="credit2">
<br /> 

3 credit hour: <input type="number" id="credit3">
<br /> 

Total: <input id="output">
<br />

<button >Calculate</button>
Posted
Updated 30-Jan-16 17:24pm
v3
Comments
Dave Kreskowiak 30-Jan-16 22:52pm    
And the problem would be ......??
Member 12297677 30-Jan-16 23:10pm    
When it calculates right now it just adds them all up. There are three fields. 1 credit hour means that if they put 5 in the field box it would calculate at 5 total. If they put 3 in the 2 credit hour field box that would be 6 credit hours total. If they put 4 in the 3 credit hour field box that would calculate at 12 credit hours total.
Dave Kreskowiak 30-Jan-16 23:23pm    
Yeah, that's exactly what you told the code to do. You still haven't said what the problem is and what the expectations of the code are. What is the logic involved supposed to be and what the fields are supposed to represent.

We can't read your mind or see your screen. You have to tell us these things as if we are completely blind because we are.
Member 12297677 30-Jan-16 23:25pm    
I updated the question and included my JS Fiddle file so you can see what it looks like.
Dave Kreskowiak 30-Jan-16 23:29pm    
OK, after reading the updated question, this is just very simple math.

You're not multiplying the values from the fields by their respective number of credit hours for that field.

1 solution

Check this out: JavaScript Arithmetic[^]
 
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