I’m building a site for the local flight school and they would like me to develop a weight and balance calculator for the site. The Calculator is very simple, it would be laid out like this:
Weight Arm Moment
Empty Plane # # #
Fuel # # #
Passengers # # #
____________________________________________
Total # # #
The #'s would be fields that visitors would enter into the form, as soon as two of the three (weight, arm, moment) are entered, the third would be calculated, using Arm = Moment / Weight.
The “Total” row would divide the total moment by the total weight and give the total arm (Center of Gravity).
The other twist is, I’d like to be able to take the “Total weight” and “Total Arm” data that was calculated, and use it for a css value on the same page. For example, (<div style=“position: absolute; left: ~TOTAL WEIGHT~; top: ~TOTAL ARM~;”>).
I hope this is possible and thanks in advance for any help!
Ahh, well, I don’t actually have any code yet. I’m not very versed in javascript, I can manipulate existing code decently, but writing it from scratch is a bit out of my league.
I could create the html form I would like, and post it here? Then you could just inject the javascript where it needs to go?
nice try but I normally try to avoid spoon feeding code (for free at least ;)) and instead prefer to help people I can see at least have had a go at coding up what they need.
I am assuming you want to learn how to do this yourself and are not simply looking for some code to copy and paste into some school homework or whatever.
No this definitely isn’t a school assignment. I’m a freelance developer. Ok I’ll look at the J3 schools. They taught me a lot of php. BTW, could this be done in php? I’m much more familar with it.
no offence, but the fact you’re asking if it can be done in php I’m wondering just how much they taught you because you can do it all with some basic php instead of javascript.
as I said it wasn’t meant to be an attack on you but I can see how it could have been taken that way.
so to answer your question
BTW, could this be done in php? I’m much more familar with it
.
yes it can and it should take no more than a dozen lines or so of some basic php number crunching and then output the results to css or wherever they need to go.
Calculating the x and y offsets can become tricky, but it can be done for each axis by using two values from the image, and the corresponding pixel positions from your intended image.
For example: the image has horizontal lines for 1600 pounds and 1800 pounds.
The 1600 line is at 250 pixels, and the 1800 line is at 170 pixels.
A difference of 200 pounds results in a change of 80 pixels, so the scaling factor is 80/200. Because we want the vertical scale to have numbers on the bottom, we use a negative scale, resulting in a vertical scale of -80/200
Then we just need an offset.
1600 * -80/200 = -640
We need it to get to 250 pixels, so we need to add 890.
Check for 1800, which needs to get to 170 pixels
1800 * -80/200 = -720
-720 + 890 = 170
In this manner, we can calculate a scale and offset for both the x and y coordinates.
this might also help you get started if doing it in php.
I am assuming you already have the html input form done.
<?php
/*
* put your form data validation
* and variable assignments code here
*/
//calculate the missing value for empty plane
if (!isset($ep_weight) || empty($ep_weight)) {
$ep_weight = $ep_moment / $ep_arm;
} else if (!isset($ep_arm) || empty($ep_arm)) {
$ep_arm = $ep_moment / $ep_weight;
} else if (!isset($ep_moment) || empty($ep_moment)) {
$ep_moment = $ep_arm * $ep_weight;
}
/*
* repeat the above if-elseif to calculate the missing values
* for fuel and passengers
*/
//at this stage all 9 values should exist
//now calculate the totals
$total_weight = $ep_weight + $f_weight + $p_weight;
$total_arm = $ep_arm + $f_arm + $p_arm;
$total_moment = $ep_moment+$f_moment+$p_moment
?>
if you need any more help, post your code and we can try to help you fix it.
How do I hide the marker when it’s position is outside of the graph? For example, load up the above code, and set empty plane weight for 3000 and empty plane moment to 80000.
Do you see any code consolidation opportunities in what I have? I know I have onclick=, onKeyPress, and readonly=true on several <input>s. Is there a way to consolidate these?
Where would I put
.toFixed(2)
in order to have the arm’s only calculate to two decimal places?
Other than those three things I’d say we’re pretty much there! Thank you guys both so much for the help!
Here we hide the target, and only when we’re within the range do we show it.
function updateTarget(target, config, totals) {
target.style.display = 'none';
x = ...
y = ...
if (x < config.x.min || x > config.x.max) {
return;
}
if (y < config.y.min || x > config.y.max) {
return;
}
target.style.display = '';
target.style.left = ...
target.style.top = ...
}
Yes there is. Get those [censored] inline scripting events out of your [redacted] HTML code.
Instead of those, you could use something like this instead:
form.onclick = function (evt) {
evt = evt || window.event;
var targ = evt.target || ev.srcElement;
if (targ.nodeName === 3) { // #text node
targ = targ.parentNode;
}
if (targ.type !== 'text') {
return;
}
// check for moment or weight
if (targ.name.search('moment') > -1 || targ.name.search('weight') > -1) {
targ.value = '';
}
}
But you don’t want to do that, (which is why it’s only in a code block) as there will be a lot of duplication with the onkeypress event too. Instead you want to move the moment/weight check out to a separate function:
function whenMomentOrWeight(evt, func) {
evt = evt || window.event;
var targ = evt.target || ev.srcElement;
if (targ.nodeName === 3) { // #text node
targ = targ.parentNode;
}
if (targ.type !== 'text') {
return;
}
// check for moment or weight
if (targ.name.search('moment') > -1 || targ.name.search('weight') > -1) {
return func(evt, targ);
}
}
So that you can give each event a separate function to do the needed work:
You are a golden god. I’ll implement these changes shortly and get back to you. Do you have any recommendations for javascript training? I really need to step my game up on javascript. I’m familiar with both javascript and php as far as pushing it around, (I do primarily wordpress sites) but I’m terrible at writing it. Lynda.com?
What I hear about that website is less than inspiring.
The effective way to train is to write your own code, and invite feedback on the code from other experienced people. The training occurs as you realise that there are better ways to do things, or that there are tradeoffs that you want to balance in some way.
It is the process of improving your code which is how a lot of valuable learning occurs.
Wise words. That definitely makes sense. I can say that for CSS. I never really attained full “mastery” of the language until I started forcing myself to optimize, and do things the right way. With CSS, that’s really the only way to do it considering cross-browser issues.