/    Sign up×
Community /Pin to ProfileBookmark

Calculator not working

Hello all.
I am new to Java Scripting, but I have been handed a previous employees job. My task is to create a calculator and having had little or no experience doing this I am asking for any help resolve my problem. The problem is that I used the calculator that was already in place and made the modifications to that one and it works to a certain excelpt for the last statment at the bottom (see below). I have been staring for hrs like creator said no help Can anyone please help?


_________________________________________________________________

function load()
{
}

function count()
{
var elprice = 4.49;// look at description of calculate_hs.js for details about prices…
var igprice = 3.99; // The code is a pain.. i wrote if statements for an automatic discount of 1$ each 2 products
var egprice = 4.49; // Just stare for a while and you will figure it out
var esprice = 5.99;
var ewprice = 4.99;
var ilprice = 2.49;
var coolprice = 9.99;
var heatprice = 9.99;
var heatpump = 17.99;
var whprice = 4.99;

if (calc.el.checked){
var wel = document.calc.el.value = elprice;
} else {
var wel = document.calc.el.value = 0;
}

if (calc.ig.checked){
var wig = document.calc.ig.value = igprice;
} else {
var wig = document.calc.ig.value = 0;
}

if (calc.eg.checked) {
var weg = document.calc.eg.value = egprice;
} else {
var weg = document.calc.eg.value = 0;
}

if (calc.ew.checked) {
var wew = document.calc.ew.value = ewprice;
} else {
var wew = document.calc.ew.value = 0;
}

if (calc.es.checked) {
var wes = document.calc.es.value = esprice;
} else {
var wes = document.calc.es.value = 0;
}

if (calc.il.checked) {
var wil = document.calc.il.value = ilprice;
} else {
var wil = document.calc.il.value = 0;
}

if (calc.cool.checked) {
var wcool = document.calc.cool.value = coolprice;
} else {
var wcool = document.calc.cool.value = 0;
}

if (calc.heat.checked) {
var wheat = document.calc.heat.value = heatprice;
} else {
var wheat = document.calc.heat.value = 0;
}

if (calc.heatpump.checked) {
var wheatpump = document.calc.heatpump.value = heatpump;
} else {
var wheatpump = document.calc.heatpump.value = 0;
}

if (calc.wh.checked) {
var wwh = document.calc.wh.value = whprice;
} else {
var wwh = document.calc.wh.value = 0;
}

if (calc.el.checked){
var del = document.calc.el.value = 1; //code for discounts
} else {
var del = document.calc.el.value = 0;
}

if (calc.ig.checked){
var dig = document.calc.ig.value = 1;
} else {
var dig = document.calc.ig.value = 0;
}

if (calc.eg.checked) {
var deg = document.calc.eg.value = 1;
} else {
var deg = document.calc.eg.value = 0;
}

if (calc.ew.checked) {
var dew = document.calc.ew.value = 1;
} else {
var dew = document.calc.ew.value = 0;
}

if (calc.es.checked) {
var des = document.calc.es.value = 1;
} else {
var des = document.calc.es.value = 0;
}

if (del + dig + deg + dew + des <= 1){
var discount = 0;
} else {
var discount = del + dig + deg + dew + des – 1;
}

if (wwh + wheatpump >18){
var discount2 = 1;
} else {
var discount2 = 0;
}

document.calc.pay.value = wel + wig + weg + wew + wes + wil +
wcool + wheat + wheatpump + wwh – discount-discount2;
}

to post a comment
JavaScript

14 Comments(s)

Copy linkTweet thisAlerts:
@JMRKERJan 18.2008 — In your assignments you do this:
[code=php]

if (calc.el.checked){
var wel = document.calc.el.value = elprice;
} else {
var wel = document.calc.el.value = 0;
}

if (calc.ig.checked){
var wig = document.calc.ig.value = igprice;
} else {
var wig = document.calc.ig.value = 0;
}
[/code]


I'm not sure about this, but to make it clearer consider your assignments like this:
[code=php]
var wel = 0;
document.calc.el.value = 0;
if (document.calc.el.checked) { wel = document.calc.el.value = elprice; }
var wig = 0;
document.calc.ig.value = 0;
if (document.calc.ig.checked) { wig = document.calc.ig.value = igprice; }
[/code]

1. I don't know if '0' is assigned to both 'wel' and 'document.calc.el.value' in the single statement.

2. You left off the 'document' in the 'if (calc.....)' checks

3. Your code doesn't specify if the 'checked' test is for a radio button or checkbox. Would make a difference on your outcome.

4. Javascripts usually passes strings, so your '.. + ...' may leave to concatenation rather than addition.

5. Stick in a few temporary 'alert()' messages to see if the values are as you expect.

6. Have you been looking at the error console for any messages?

Sorry for the confusion, but the amount of code you attached leaves me with more questions than I have answers at this time.

?
Copy linkTweet thisAlerts:
@mbenitauthorJan 18.2008 — It is for a checked box and the box looks like this
Copy linkTweet thisAlerts:
@JMRKERJan 18.2008 — Huh?
Copy linkTweet thisAlerts:
@mbenitauthorJan 18.2008 — First off thank you very much for your quick response, I really appreciate you taking the time to respond.

It is for a checked box, which you can choose multiple items and it then generates a amount with a 1$ discount below. But whenever I choose 17.99 and some other tiime a result with decimals. Like I choose $17.99 and $4.99 i get this result (21.979999999999997) and it only happens when I choosee $17.99 and any other item.
Copy linkTweet thisAlerts:
@JMRKERJan 18.2008 — That's a round-off error and will occur with almost every computer and calculator in existance.

Several methods to fix this: Math.round(), toFixed() come to mind, but there are other functions.

Search this forum (or google it) for many discussions on this.
Copy linkTweet thisAlerts:
@mbenitauthorJan 18.2008 — Thank you for your response I will try these and let you know. Thank you, Thank you.
Copy linkTweet thisAlerts:
@mbenitauthorJan 18.2008 — I am sorry to bother again, but could you tell me where to put the these to commands in the script?
Copy linkTweet thisAlerts:
@JMRKERJan 18.2008 — I'm only guessing here as you have not anwered the other questions yet and I don't know what the rest of your HTML and JS looks like, but I would try here:
[code=php]
var totals = wel + wig + weg + wew + wes + wil +
wcool + wheat + wheatpump + wwh - discount-discount2;
document.calc.pay.value = totals.toFixed(2);
// or possibly
// document.calc.pay.value = Math.round(totals);
[/code]


See: http://www.pageresource.com/jscript/j_a_03.htm

for more information. ?
Copy linkTweet thisAlerts:
@mbenitauthorJan 18.2008 — This is what the calculate page looks like, see attached picture

[upl-file uuid=ac577886-a3ef-4b22-8190-28badd1cc7c1 size=92kB]calculate.bmp[/upl-file]
Copy linkTweet thisAlerts:
@JMRKERJan 19.2008 — I don't know where to tell you to put the code in the picture.

I need to see the code itself.

Do you have a link to a test HTML site we could look at?


It does not need to be your true site, just one that tests the checkbox totals logic without saving the information anywhere.
Copy linkTweet thisAlerts:
@mbenitauthorJan 21.2008 — That first post I put out is the entire code.
Copy linkTweet thisAlerts:
@JMRKERJan 22.2008 — Post #1 can't be the entire code. It is only the JS portion. Where is the calling HTML script? The image you posted (#10) only shows how you want to see it, not how it was designed. There is an interaction between the HTML and JS that is not being provided and that is probably where the problem lies.
Copy linkTweet thisAlerts:
@mbenitauthorJan 22.2008 — I couldn't find a web site we could mutually work from, but I attached the calculator html file had to save it as test to upload it.

[upl-file uuid=a5339dbb-cd78-44c9-9a17-fa0ad9aa94d7 size=10kB]calculate.txt[/upl-file]
Copy linkTweet thisAlerts:
@JMRKERJan 22.2008 — In the last post, there is a reference in the file to:
[code=php]
<script language="JavaScript1.1" src="calculate_FE.js"></script>
[/code]

This file is missing as I am getting a "load" error in the body statement.

JS will not work unless all errors are resolved.


Post the 'calculate_FE.js' file so it can be referenced in the last file (calculate.txt) you posted.

Then I can take a look at ALL the code together. ?
×

Success!

Help @mbenit 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 5.22,
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: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

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