Building a Java Script calculator for Electrics

Joined
27 Jan 2008
Messages
24,313
Reaction score
2,777
Location
Llanfair Caereinion, Nr Welshpool
Country
United Kingdom
Years ago I built a Excel program to check the readings obtained after doing electrical tests and flag up faults. However times have moved on and I need to pay to have Excel on my phone so using Java Script means any device with a browser will do the calculation.

I have struggled was not as easy as I expected main problem was addition where is x = 1 and y = 1 then x + y = 11 rather than 2. OK got around that problem with eval but next is the printing.

This calculator does nearly what I want but what would add the cream to cake would be a print button where I can print the results minus all the info not normally given with a Schedule of test results and I just don't know where to start.

My attempts have gone full circle with.
Volt drop from Cable length Calculator
Volt drop from Cable length Calculator
olt drop and Cable length from Prospective Short Circuit Current Calculator
Volt drop and Cable length from loop impedance Calculator
Each one slightly different and although close clearly there are errors I think from use of parseInt instead of eval in early versions.

Can anyone give me some pointers. One advantage of the Java Script calculator is I can ask others to test it with Excel it was so hard to find anyone who could test it and I know know I made an error with ring mains with Excel version corrected with Java Script version.

But maths is not my strong point I failed "A" level maths although I did manage to do it well enough to pass my Foundation Degree in Electrical and Electronic Engineering think the lectures took pity on me!

Logs and Calculus I and not too good at imaginary numbers I found easy and statistics is some form of magic and I really haven't a clue.

So trying a keep it simple silly approach any advice appreciated.
 
Hi,

A few thoughts from me:

1) Indent your code correctly! I looked at your indentation and ... ***ughhh*** !!!! (Sorry).

2) I am not sure why you are using eval. The purpose of eval is simply to evaluate a string as if it were javascript code. Furthermore, the way you have used eval introduces SERIOUS security errors into your code. Specifically: By calling eval on the value of a form input, you are inviting the user to inject whatever kind of javascript they like into your code. This is bad. And there are hackbots which WILL try and do that.

3) I imagine that your errors are coming about because you are using a mixture of integers and floating point numbers. You need to cast all your input values to floating point numbers using parseFloat upon input. So, for example:

[code:1]var im = parseFloat(document.getElementById('im').value);//Installation Method.[/code:1]

You can cast your hardcoded values to float by appending a decimal zero to each integer literal. E.g.:

[code:1]44.0 //If you type 44, you will create an integer, not a float.[/code:1]

I hope that helps!
 
Back
Top