Solved

JavaScript/jquery function needed from existing code

Posted on 2014-04-15
5
592 Views
Last Modified: 2014-04-22
Hello :)
I'm  pulling my hair out with this one ;)
I am trying to create a JavaScript/jquery function that will return a calculated value based on parameters passed.

There is a site here : http://bikecalculator.com that does exactly what I need. It's written in JavaScript and the source is viewable. I need a function where I can pass the parameters that the screen asks for and it returns the POWER WATTS.

Does anyone know of an existing function that does this or can someone build one from the example site?

My gratitude will take the form of 500 points and a PayPal donation :)

Thanks in advance
0
Comment
Question by:jellydeal
  • 2
  • 2
5 Comments
 
LVL 22

Expert Comment

by:Mrunal
ID: 40001049
Hi,
if you can find existing JavaScript function with developer toolbar of by saving same html page.

if you look into this, you will find that from all controls (textboxes, dropdownlist)

following function is get called:

update(calcID)
and
updateDistance(calcID,1)
0
 

Author Comment

by:jellydeal
ID: 40001223
Hi,
Thanks I did do this and tried to Create a function myself using the existing code but it wouldn't work. I need a function to get called hundreds of times and the GUI slider thing on that page stalls the whole thing. I ideally just want a function .. I failed myself :(
0
 
LVL 22

Expert Comment

by:Mrunal
ID: 40001374
Hi, can you share your code, till now what you have done.

for dragging functionality, use this:

http://skidding.github.io/dragdealer/
0
 

Author Comment

by:jellydeal
ID: 40001395
Hi sure ;
http://www.countrycorks.co.uk/bike/calc.htm
It processes a file, test.gpx and after pressing process, and waiting it reads in some data. Populates the form fields ,calls the update() but the watts all come out at 150. I got to the point of thining maybe it's best to reinvent the wheel on this occasion!
0
 
LVL 6

Accepted Solution

by:
Brijesh Gandhi earned 500 total points
ID: 40014529
 <script>

		tireValues = [0.005, 0.004, 0.012];
		aeroValues = [0.388, 0.445, 0.420, 0.300, 0.233, 0.200];

function makeDecimal2 (value) {
	if (value != null && value != 0) {
		var x = Math.round(parseFloat(value) * 100);
		var d;
		if (x < 100) d=0;
		else d = parseInt(x / 100);
		var c = x % 100;
		var g = (c >= 10)?"":"0";
		value = "" + d + "." + g + c;
	}
	return value
}

function makeDecimal0 (value) {
	if (value != null && value != 0) {
		value = "" + Math.round(parseFloat(value));
	}
	return value
}

function valDecNumber(str) {  /* returns true if a valid number */
	if(str.length == 0) return false;
	var noNegSignYet = true;
	var noPointYet = true;
	for (i=0; i < str.length; i++) {
		var chr = str.substring(i, i+1)
		if (noPointYet && chr == ".") noPointYet = false;
		else if (noNegSignYet && chr == "-") noNegSignYet = false;
		else if (chr< "0" || chr > "9") {
			alert ("I don't think you want to do that, Dave.");
			return false
		}
	}
	return true
}

function empty(inputStr) {
	if (inputStr == "" || inputStr == null) return true;
	return false
}


function update(form) {

	with (document.f) {
		
		
		/* USUnits = US.checked; */
		USUnits = 0;
		/* baseMetabolism = baseMet.checked; */
		
		/* Left panel */
		vv = eval(velocity.value) / 3.6;  // converted to m/s;
		rweightv = eval(rweight.value);
		bweightv = eval(bweight.value);

		
		theTire = tire.selectedIndex;
		rollingRes = tireValues[theTire];
		theAero = aero.selectedIndex;
		frontalArea = aeroValues[theAero];
		gradev = eval(grade.value) * 0.01;
		headwindv = eval(headwind.value) / 3.6;  // converted to m/s
		distancev = eval(distance.value);
		temperaturev = eval(temperature.value);
		elevationv = eval(elevation.value);
		transv = eval(trans.value) * 0.01;
		
		if(USUnits) {      // Convert to metric units
			rweightv *= 0.4536;
			bweightv *= 0.4536;
			vv *= 1.6092;
			headwindv *= 1.6092;
			distancev *= 1.6092;
			temperaturev = (temperaturev - 32) * 0.555;
			elevationv *= 0.3048;
		}
		
		
		
		/* Left Calculation */
		density = (1.293 - 0.00426 * temperaturev) * Math.exp(-elevationv / 7000.0);
		twt = 9.8 * (rweightv + bweightv);  // total weight in newtons
		A2 = 0.5 * frontalArea * density;  // full air resistance parameter
		tres = twt * (gradev + rollingRes); // gravity and rolling resistance
		
		tv = vv + headwindv; 
		pv = (vv * tres + vv * tv * tv * A2) / transv;
		
		if (vv > 0.0) t = 16.6667* distancev / vv;  // vv is m/s here
			else t = 0.0;  // don't want any div by zero errors
		c = t / 60.0 * (pv * 4.0) / 1.163;  
			/* 1.163 is for converting watt-hours to kilocalories */
			/* energy conversion eff = 0.25 */
		wl = c / 7716;
		
		if(USUnits) {     // Convert to US units
			wl *= 2.205;
		}
		
		/* Left results */
		power.value = makeDecimal0(pv);
		time.value = makeDecimal2(t);
		calories.value = makeDecimal0(c);
		weightloss.value = makeDecimal2(wl);
		
		
	}
}
</script>

Open in new window


Above Code is calling .Related URL is Below
Site URL
I hope u get your point .Try to pass value in Update function and get result of Power WAtt
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Requirements JQuery 1.6+ HTML CSS Introduction This article was inspired by an EE question (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_28372511.html) on how to make a page show some balloons animate up a page…
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

816 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now