PDA

Click to See Complete Forum and Search --> : Javascript equivilant


Mage
Jul 18th, 2003, 05:48 AM
Can someone please tell me what the the Javascript equivilant of this VB Code is


i = 1.33
i=int(i)


I want to lose the decimal places.

Thanks

dj4uk
Jul 18th, 2003, 06:54 AM
var i = 1.33
i = parseInt(i)

This will return an integer type but it won't round up -> 1.9 will return 1.

var i = 1.33
i = Math.round(i)

Rounds the number but I'm not 100% sure if it returns an integer type - might need to parseInt afterwards.

Mage
Jul 18th, 2003, 07:09 AM
Thanks.

But i am also getting this error:

TypeError: getField("In") has no properties
3:Document-Level:TotalTime

this is what i am sending


event.value=totalTime("Text2","Text3","Text5");


where Text2 = 12:00
Text3=17:00
Text5= 01:15


function totalTime(In,Out,Lunch)
{
var start=getField("In").value;
var finish=getField("Out").value;
var lunch=getField("Lunch").value;
var sa=start.split(":");
var fa=finish.split(":");
var la=lunch.split(":");
var startminutes=(sa[0]*60)+(sa[1]*1);
var finishminutesa=(fa[0]*60)+(fa[1]*1);
var lunchminutes=(la[0]*60)+(la[1]*1);
if (finishminutesa <= startminutes) {
var finishminutes=finishminutesa+720
}
else if ((finishminutesa-startminutes-lunchminutes)/60<=0){
var finishminutes=finishminutesa+720
}
else {
var finishminutes=finishminutesa
};
var totalHours=parseint(finishminutes-startminutes-lunchminutes)/60;
if(isNaN(totalTime))totalTime=0;
var totalMin=((finishminutes-startminutes-lunchminutes)/60)-TotalHrs;

var displaytotal=totalHours +":"+(totalMin*60)

return displaytotal;
}


I just dont get it....

dj4uk
Jul 18th, 2003, 07:23 AM
Ok so you're passing 3 variables into the function totalTime and then trying to access these values using:

var start=getField("In").value;
var finish=getField("Out").value;
var lunch=getField("Lunch").value;

Try using
var start = In
var finish = Out
var lunch = Lunch

Might be better to rename two of these variables from IN and OUT not sure if they are reserved words.

Mage
Jul 18th, 2003, 07:44 AM
You are the "Man"... Thanks

But one more quick question what would

cStr()
Left("String",2)
Right("String",2)
Mid(("String",1,3)

be in javascript, or if you know a site that would have all this stuff.

Thanks

Mage
Jul 18th, 2003, 08:14 AM
--- dont worry about this part i found the problem-----

I am just getting NaN:NaN returned any ideas

[Java Script]
function TotalTime(StartTime,FinishTime,Lunch)
{
var start=StartTime;
var finish=FinishTime;
var lunch=Lunch;
var sa=start.split(":");
var fa=finish.split(":");
var la=lunch.split(":");
var startminutes=(sa[0]*60)+(sa[1]*1);
var finishminutesa=(fa[0]*60)+(fa[1]*1);
var lunchminutes=(la[0]*60)+(la[1]*1);
if (finishminutesa <= startminutes) {
var finishminutes=finishminutesa+720
}
else if ((finishminutesa-startminutes-lunchminutes)/60<=0){
var finishminutes=finishminutesa+720
}
else {
var finishminutes=finishminutesa
};
var totalHours=parseInt(finishminutes-startminutes-lunchminutes)/60;
if(isNaN(totalTime))totalTime=0;
var totalMin=((finishminutes*1-startminutes*1-lunchminutes*1)/60)-totalHours;

var displaytotal=totalHours + " " + finishminutes + " " + startminutes + " " + lunchminutes

return displaytotal;
}
[/Java Script]

dj4uk
Jul 18th, 2003, 10:47 AM
A good site for refrence is http://www.devguru.com.

What are the values being passed into the function? If you are getting NaN (not a number) returned then you might be having problems with datatypes. Left, Right and Mid can only be used on strings.

Mage
Jul 18th, 2003, 10:50 AM
I managed to solve the NaN problem, thanks do you know what the equivilant of Len() is??

dj4uk
Jul 18th, 2003, 11:14 AM
Len(variablename)

would be

variablename.length

Mage
Jul 21st, 2003, 09:10 AM
Thanks
but how would you pad out a string/Number?? without having to finding out the length of the string

string = Right("00" & string,2)

the only thing I can fing is substring but that would be the same as mid in which case you woul need to do if statements to find out how long the string is and the substring it accordingly.

dj4uk
Jul 21st, 2003, 09:15 AM
Not exactly sure what you are trying to do could you explain again.

Mage
Jul 21st, 2003, 09:19 AM
if you had and account number that needed to be 6 characters long. and the user types in 123, but you would need to display it/ store it as 000123 in VB

sacc="123"
you would just sacc= right("000000" & sacc,6)
sacc = 000123

how would you do that with javascript
hope that is better.

dj4uk
Jul 21st, 2003, 09:26 AM
You could use the slice function http://www.devguru.com/Technologies/ecmascript/quickref/string_slice.html but there is no direct conversion of right.

Mage
Jul 21st, 2003, 09:45 AM
thanks, i thought as much.

what is the definitions for the substring()
ie mid(string,(start),(for the len of)

as i am getting wierd stuff back from my substring just doesnt seem to want to follow a rule