ClioSport.net

Register a free account today to become a member!
Once signed in, you'll be able to participate on this site by adding your own topics and posts, as well as connect with other members through your own private inbox!

  • When you purchase through links on our site, we may earn an affiliate commission. Read more here.

Java Script Help



Andy_con

ClioSport Club Member
  clio 182
so this is my sum (taken from an excel spreadsheet) - =(B13*B14)+(B13*B13)/(2*(B15+(0.1*B16)))

someone wrote some java script for me to do this same sum, but im not getting the right result, can anyone help as to why?



/**
* Notification that the UI is about to transition to a new page.
* Perform custom prepage-transition logic here.
* @param {String} currentPageId
* @param {String} targetPageId
* @returns {boolean} true to continue transtion; false to halt transition
*/
phoneui.prePageTransition = function(currentPageId,targetPageId) {
// add custom pre-transition code here
// return false to terminate transition
return true;
}
/**
* Notification that the UI has transition to a new page.
*
* @param {String} newPageId
*/
phoneui.postPageTransition = function(newPageId) {

}
/**
* Notification that device orientation has changed.
*
* @param {String} newOrientation
*/
phoneui.postOrientationChange = function(newOrientation) {

}
/**
* Called when document is loaded.
*/
phoneui.documentReadyHandler = function() {
}

function updateTotal() {
//read arg1-arg4 input values
// inspect the html file to get the field IDs
var arg1 = parseInt($('#m1-sum-arg1Field').val(), 10);
var arg2 = parseInt($('#m1-sum-arg2Field').val(), 10);
var arg3 = parseInt($('#m1-sum-arg3Field').val(), 10);
var arg4 = parseInt($('#m1-sum-arg4Field').val(), 10);
//convert all non-numeric (NaN) inputs to 0
arg1 = isNaN(arg1) ? 0 : arg1;
arg2 = isNaN(arg2) ? 0 : arg2;
arg3 = isNaN(arg3) ? 0 : arg3;
arg4 = isNaN(arg4) ? 0 : arg4;
//compute sum of args
sum = (arg1*arg2)+(arg1*arg1)/(2*(arg3+(0.1*arg4)));
//display total
$('#m1-sum-totalText').text(sum);
}

function clearArg(argNum) {
id = '#m1-sum-arg' + argNum + 'Field';
$(id).val('');
updateTotal();
}


so the two sums side by side

=(B13*B14)+(B13*B13)/(2*(B15+(0.1*B16)))
=(arg1*arg2)+(arg1*arg1)/(2*(arg3+(0.1*arg4)));
 

Andy_con

ClioSport Club Member
  clio 182
yes that is correct.

no idea what passing something means as i know nothing about java, could you explain a little more?
 
  182/RS2/ Turbo/Mk1
I mean that the values from the spreadsheet arent ending up in the right arguments in the javascript if that is giving you the wrong result.
 

Andy_con

ClioSport Club Member
  clio 182
what a ball ache, everything is spot on i cant find any errors

grrr.............
 

Andy_con

ClioSport Club Member
  clio 182
1 = 13.41
2 = 1.50
3 = 4.41
4 = 10

my spreadsheet gives 36.73
that java gives 29.9
 
  182/RS2/ Turbo/Mk1
So what it should be is:

(13.41*1.50)+(13.41*13.41)/(2*(4.41+(0.1*10)))

=
(20.115) + (179.8281)/(10.82)

=
20.115 + 16.62

= 36.735



So I reckon for some reason the javascript isnt processing the brackets properly.

Change it to
(arg1*arg2)+((arg1*arg1)/(2*(arg3+(0.1*arg4))));

And tell me what it says then?
 
  182/RS2/ Turbo/Mk1
Can you get it to output the variables.

Not sure if alert works in the context you are using this, but if it does can you put the line

alert("arg1=" + arg1 + "; arg2=" + arg2 + "; arg3=" + arg3 + "; arg4=" + arg4);



(just before the sum would be ideal place)


And let me know what it says if it does let you do that.
 

sn00p

ClioSport Club Member
  A blue one.
parseInt. You can't use that, you need floating point.

Edit:

Replace them with parseFloat or parseDouble.
 

Andy_con

ClioSport Club Member
  clio 182
java not being my thing, like this?

//compute sum of args
sum = alert("arg1=" + arg1 + "; arg2=" + arg2 + "; arg3=" + arg3 + "; arg4=" + arg4); (arg1*arg2)+((arg1*arg1)/(2*(arg3+(0.1*arg4))));


Can you get it to output the variables.

Not sure if alert works in the context you are using this, but if it does can you put the line

alert("arg1=" + arg1 + "; arg2=" + arg2 + "; arg3=" + arg3 + "; arg4=" + arg4);



(just before the sum would be ideal place)


And let me know what it says if it does let you do that.
 

sn00p

ClioSport Club Member
  A blue one.
Code:
[COLOR=#333333]var arg1 = parseFloat($('#m1-sum-arg1Field').val());[/COLOR]
[COLOR=#333333]var arg2 = parseFloat($('#m1-sum-arg2Field').val());[/COLOR]
[COLOR=#333333]var arg3 = parseFloat($('#m1-sum-arg3Field').val());[/COLOR]
[COLOR=#333333]var arg4 = parseFloat($('#m1-sum-arg4Field').val());

[/COLOR]
 
  182/RS2/ Turbo/Mk1
java not being my thing, like this?

//compute sum of args
sum = alert("arg1=" + arg1 + "; arg2=" + arg2 + "; arg3=" + arg3 + "; arg4=" + arg4); (arg1*arg2)+((arg1*arg1)/(2*(arg3+(0.1*arg4))));

Yes like that, then we can see what values are ending up in what variables.
 
  182/RS2/ Turbo/Mk1
Code:
[COLOR=#333333]var arg1 = parseFloat($('#m1-sum-arg1Field').val());[/COLOR]
[COLOR=#333333]var arg2 = parseFloat($('#m1-sum-arg2Field').val());[/COLOR]
[COLOR=#333333]var arg3 = parseFloat($('#m1-sum-arg3Field').val());[/COLOR]
[COLOR=#333333]var arg4 = parseFloat($('#m1-sum-arg4Field').val());[/COLOR]



Well spotted, its rounding off when its using parseInt.
 

Andy_con

ClioSport Club Member
  clio 182
Code:
[COLOR=#333333]var arg1 = parseFloat($('#m1-sum-arg1Field').val());[/COLOR]
[COLOR=#333333]var arg2 = parseFloat($('#m1-sum-arg2Field').val());[/COLOR]
[COLOR=#333333]var arg3 = parseFloat($('#m1-sum-arg3Field').val());[/COLOR]
[COLOR=#333333]var arg4 = parseFloat($('#m1-sum-arg4Field').val());[/COLOR]




ok so does that replace the bit in bold, or does that bit you said need to go in as wel??

/**
* Notification that the UI is about to transition to a new page.
* Perform custom prepage-transition logic here.
* @param {String} currentPageId
* @param {String} targetPageId
* @returns {boolean} true to continue transtion; false to halt transition
*/
phoneui.prePageTransition = function(currentPageId,targetPageId) {
// add custom pre-transition code here
// return false to terminate transition
return true;
}
/**
* Notification that the UI has transition to a new page.
*
* @param {String} newPageId
*/
phoneui.postPageTransition = function(newPageId) {

}
/**
* Notification that device orientation has changed.
*
* @param {String} newOrientation
*/
phoneui.postOrientationChange = function(newOrientation) {

}
/**
* Called when document is loaded.
*/
phoneui.documentReadyHandler = function() {
}

function updateTotal() {
//read arg1-arg4 input values
// inspect the html file to get the field IDs
var arg1 = parseInt($('#m1-sum-arg1Field').val(), 10);
var arg2 = parseInt($('#m1-sum-arg2Field').val(), 10);
var arg3 = parseInt($('#m1-sum-arg3Field').val(), 10);
var arg4 = parseInt($('#m1-sum-arg4Field').val(), 10);

//convert all non-numeric (NaN) inputs to 0
arg1 = isNaN(arg1) ? 0 : arg1;
arg2 = isNaN(arg2) ? 0 : arg2;
arg3 = isNaN(arg3) ? 0 : arg3;
arg4 = isNaN(arg4) ? 0 : arg4;
//compute sum of args
sum = (arg1*arg2)+((arg1*arg1)/(2*(arg3+(0.1*arg4))));

//display total
$('#m1-sum-totalText').text(sum);
}

function clearArg(argNum) {
id = '#m1-sum-arg' + argNum + 'Field';
$(id).val('');
updateTotal();
}
 

sn00p

ClioSport Club Member
  A blue one.
It's the calculation for how far drivers need to see ahead (of a hazard) so that they can stop (safely) from a given speed.
 

Andy_con

ClioSport Club Member
  clio 182
im trying to make an iphone app but its going very very slowly.

i know nothing about java and im trying to use mobione
 

Andy_con

ClioSport Club Member
  clio 182
so who wants to offer their services to help me get this app finished?

(its not an attempt to become rich, just a good talk point for future jobs)
 
As said, ParseInt would round up/down at each calculation in the formula, leaving you with an incorrect result at the end.

ParseFloat / ParseDouble will keep the decimal point in.
 

Andy_con

ClioSport Club Member
  clio 182
are you a highways consultant?

so wana give me a hand?

you seem like the perfect person, know what im talking about and know about java
 

sn00p

ClioSport Club Member
  A blue one.
are you a highways consultant?

so wana give me a hand?

you seem like the perfect person, know what im talking about and know about java

No, I wrote the software for a radar that detects vehicles (VAS use etc) - some models have data collection, so analysis software was also needed to collect, process and analyse the data.

If I had the time, I'd give you a hand, but I really don't. Sorry. I can answer the odd casual query, but I've got a million and one things to do.
 

Andy_con

ClioSport Club Member
  clio 182
ah i see figures

VAS which always go wrong ;-)

ok no worrys thanks for the offer
 

Andy_con

ClioSport Club Member
  clio 182
ok well next question to help me on my way, is there a way to predefine two inputs. but they can be changed if need be?

T and D are pretty standard where as V and A are always different.

so how do i make the cell for T say 1.50 and D say 4.41 as standard???
 

sn00p

ClioSport Club Member
  A blue one.
ah i see figures

VAS which always go wrong ;-)

ok no worrys thanks for the offer

We've just designed a retrofit radar module for use in a competitors product....their product has a high failure rate so we've been told! May as well make some cash out of their product!
 

sn00p

ClioSport Club Member
  A blue one.
ok well next question to help me on my way, is there a way to predefine two inputs. but they can be changed if need be?

T and D are pretty standard where as V and A are always different.

so how do i make the cell for T say 1.50 and D say 4.41 as standard???

In document ready you'd do something like:

$('#m1-sum-arg3Field').text("1.50")

whatever fields are appropriate.
 


Top