/    Sign up×
Community /Pin to ProfileBookmark

Calculator help

Hi guys. I’m sorry, I know next to nothing about javascript and I’m muddling my way through it. Would really appreciate your help. I’m trying to make a calculator, one with sort of two mini calculators and then a third that takes the totals from the two and adds them together. Here’s what I’ve got so far:

function startCalc(){
interval = setInterval(“calc()”,1);
}
function calc(){
one = document.autoSumForm.firstBox.value;
two = document.autoSumForm.secondBox.value;
three = document.autoSumForm.thirdBox.value;
four = document.autoSumForm.fourthBox.value = (one * 1) * (two * 1) * (three *

4.5);

five = document.autoSumForm.fifthBox.value;
six = document.autoSumForm.sixthBox.value;
seven= document.autoSumForm.seventhBox.value = (five * 1) * (six * 1)

eight = document.autoSumForm.eighthBox.value = (four * 1) + (seven * 1)
}
function stopCalc(){
clearInterval(interval);
}

Works fine with it’s just the first four (only one calculator) but when I throw in the other two it doesn’t work at all and I don’t understand why.

to post a comment
JavaScript

8 Comments(s)

Copy linkTweet thisAlerts:
@ZumoWrestlerJul 18.2007 — Maybe something like this? I'm not exactly sure what you're going for but here is a guess....

[CODE]
function calc(){
//Guessing you have id set as following...
var one = document.getElementById('firstBox').value;
var two = document.getElementById('secondBox').value;
var three = document.getElementById('thirdBox').value;
var four = parseInt(one)*parseInt(two)*parseInt(three)*4.5;
document.getElementById('fourthBox').value = four;

var five = document.getElementById('fifthBox').value;
var six = document.getElementById('sixthBox').value;
var seven = parseInt(five)*parseInt(six);
document.getElementById('seventhBox').value = seven;

var eight = four + seven;
document.getElementById('eighthBox').value = seven;
}
[/CODE]
Copy linkTweet thisAlerts:
@jessnoonyesauthorJul 18.2007 — Hey thanks for your help. Judging by your code I was writing things wrong. I'm trying to get the first calculator working before I do anything else. It's not functional though. Any idea why?

function startCalc(){

interval = setInterval("calc()",1);

}

function calc(){

var one = document.autoSumForm.firstBox.value;

var two = document.autoSumForm.secondBox.value;

var three = document.autoSumForm.thirdBox.value;

var four = document.autoSumForm.fourthBox.value;

var five = document.autoSumForm.fifthBox.value;

var six = document.autoSumForm.sixthBox.value;

var seven = document.autoSumForm.seventhBox.value;

var eight = document.autoSumForm.eighthBox.value;

var nine = document.autoSumForm.ninthBox.value;

var ten = document.autoSumForm.tenthBox.value;

var eleven =

parseInt(one)+parseInt(two)+parseInt(three)+parseInt(four)+parseInt(five)+parseInt(si

x)+parseInt(seven)+parseInt(eight)+parseInt(nine)+parseInt(ten);

document.autoSumForm.('eleventhbox').value = eleven;


}

function stopCalc(){

clearInterval(interval);

}
Copy linkTweet thisAlerts:
@jessnoonyesauthorJul 18.2007 — Oh, do I have to write the firstbox like this: ('eighthBox')? I'll try that
Copy linkTweet thisAlerts:
@pugs421Jul 18.2007 — Whatever your trying to do, it looks like your going about it all wrong.

Can you provide the HTML as well so I can see what the goal is. Perhaps a better summary as to what you are trying to accomplish would help as well.
Copy linkTweet thisAlerts:
@ZumoWrestlerJul 18.2007 — Yeah. If you do a direct path you don't need anything in parentheses. If you do it by document.getElementById(strID) or document.getElementByName(strName) you do need the parentheses. Other then that it looks good. From what I can see.
Copy linkTweet thisAlerts:
@pugs421Jul 18.2007 — Jess,

The concepts shown in the following copy/paste code should help you alot:

[CODE]
<html>
<body>
<form name="myForm">
<input type="text" name="Input_1"><br>
<input type="text" name="Input_2"><br>
<input type="text" name="Input_3"><br>
<input type="text" name="Input_4"><br>
<input type="text" name="Input_5"><br>
<input type="text" name="Input_6"><br>
<input type="text" name="Input_7"><br>
<input type="text" name="Input_8"><br>
<input type="text" name="Input_9"><br>
<input type="text" name="Input_10"><br>
Total=<input type="text" name="Input_Total"><br>
<input type="button" name="btnSubmit" value="calculate" onclick="calculate();">
</form>
<script>
function calculate(){
var oForm = document.myForm;
var nTotal = 0;
for(var i=1;i<=10;i++){
nTotal += (oForm.elements['Input_'+i].value * 1);
}
oForm.elements['Input_Total'].value = nTotal
}
</script>
</body>
</html>

[/CODE]
Copy linkTweet thisAlerts:
@jessnoonyesauthorJul 18.2007 — Jess,

The concepts shown in the following copy/paste code should help you alot:

[CODE]
<html>
<body>
<form name="myForm">
<input type="text" name="Input_1"><br>
<input type="text" name="Input_2"><br>
<input type="text" name="Input_3"><br>
<input type="text" name="Input_4"><br>
<input type="text" name="Input_5"><br>
<input type="text" name="Input_6"><br>
<input type="text" name="Input_7"><br>
<input type="text" name="Input_8"><br>
<input type="text" name="Input_9"><br>
<input type="text" name="Input_10"><br>
Total=<input type="text" name="Input_Total"><br>
<input type="button" name="btnSubmit" value="calculate" onclick="calculate();">
</form>
<script>
function calculate(){
var oForm = document.myForm;
var nTotal = 0;
for(var i=1;i<=10;i++){
nTotal += (oForm.elements['Input_'+i].value * 1);
}
oForm.elements['Input_Total'].value = nTotal
}
</script>
</body>
</html>

[/CODE]
[/QUOTE]


This is great!! thanks to all of you for your help. It makes a lot of sense to me written out this way. The only other thing I need to do is, instead of add line one to everything else, I need to add up lines 2-10 and then multiply the answer to line one.
Copy linkTweet thisAlerts:
@pugs421Jul 18.2007 — This is a strange application that you are making. In any event, here is what you are trying to do.

[CODE]

<html>
<body>
<form name="myForm">
<input type="text" name="Input_1"><br>
<input type="text" name="Input_2"><br>
<input type="text" name="Input_3"><br>
<input type="text" name="Input_4"><br>
<input type="text" name="Input_5"><br>
<input type="text" name="Input_6"><br>
<input type="text" name="Input_7"><br>
<input type="text" name="Input_8"><br>
<input type="text" name="Input_9"><br>
<input type="text" name="Input_10"><br>
Total=<input type="text" name="Input_Total"><br>
<input type="button" name="btnSubmit" value="Add Them All" onclick="addInputs();">
<input type="button" name="btnSubmit" value="Multiply 2 - 10 Total to Input_1" onclick="multiplyInputs();">
</form>
<script>
function addInputs(){
var oForm = document.myForm;
var nTotal = 0;
for(var i=1;i<=10;i++){
nTotal += (oForm.elements['Input_'+i].value * 1);
}
oForm.elements['Input_Total'].value = nTotal
}

function multiplyInputs(){
var oForm = document.myForm;
var nTotal = 1;
for(var i=2;i<=10;i++){
if(oForm.elements['Input_'+i].value != ''){//make sure it has a value
nTotal = nTotal * (oForm.elements['Input_'+i].value * 1);
}
}
oForm.elements['Input_1'].value = nTotal
}
</script>
</body>
</html>

[/CODE]


If I had a better idea of what you were trying to accomplish, this code would undoubtably be structured differently. I was almost inclined to create a Calculator class with methods to perform the functionality that you require. It's late though....so screw u guys...I'm going home.

Good luck with your calculator and Javascript endeavor.
×

Success!

Help @jessnoonyes spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 4.28,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...