Networking | Hardware | Software | Multimedia | System | Unix&Linux | MBA

Home>>Hardware>>Can function RETURN a value?

Can function RETURN a value?

tdavis
11-23-2005, 08:43 PM
I know the answer must be yes, but I am really having a hard time figuring this out. I have a simple script below, that calculates age (I know I need to do some more work). I want to redisplay the value returned from the function. It works OK, because the result displays correctly in the alert.

I know this is simple, but what am I doing wrong?

Thanks!
-tdavis

<script type="text/javascript">
function calcAge(returnAge) {

var dob_month;
var dob_day;
var dob_year;
var dob;

dob = document.form1.elements.dob.value;

var birthDay = dob.split("-");

dob_month = birthDay[0];
dob_day = birthDay[1];
dob_year = birthDay[2];

var time=new Date();
var lmonth=time.getMonth() + 1;
var date=time.getDate();
var year=time.getYear() + 1900;

var returnAge = year - dob_year;

alert("Your age is: " + returnAge);

}
</script>


<body>

<form name="form1">

<p>Enter date of birth in format MM-DD-YYYY
<input id="dob" type="text" name="dob" value="">
</p>

<p id="itext">
Click the button below to calculate your age
</p>

<p>
<input type=submit onclick="calcAge(returnAge)">
</p>

<p>
Your age is:
<script language="Javascript">
var returnAge;
document.write(returnAge);
</script>
</p>

boxxer03
11-23-2005, 09:29 PM
I know this isn't your problem, but just by looking real quick I would change the line:
var year=time.getYear() + 1900;
to:
var year=time.getFullYear();
.getYear is outdated and with .getFullYear you don't need to add + 1900 at the end.

boxxer03
11-23-2005, 09:30 PM
var year=time.getFullYear();
that's better....

PhotoJoe47
11-23-2005, 09:32 PM
Yes you can by using the keyword "return".

Replace this line:

var returnAge = year - dob_year;

With this:

return year - dob_year;

Now when you call the function you can do it like this:

age = calcAge(dob);

I hope this helps?

boxxer03
11-23-2005, 09:35 PM
You could also change the part where it says 'Your Age is:' and put in a text box there and display the results there, so you don't have to use another jscript. You could set it to readonly and use the style="" attribute to remove the borders fromt he textbox so you'd never know it was there.

tdavis
11-23-2005, 09:55 PM
That is exactly what I need to do, but I am struggling with this just a bit.
Would I eliminate the submit button and replace it with a text field?

boxxer03
11-23-2005, 10:04 PM
No, just change this code. Cause you still need the submit button for the function to be called and work:
<p>
Your age is:
<script language="Javascript">
var returnAge;
document.write(returnAge);
</script>
</p>

try something like:

<p>
Your age is: <input type="text" name="r_age" size="10" readonly style="border:0px solid black; background-color:transparent;">
</p>
</form>

and then change the code in your script to display it:

<script type="text/javascript">
function calcAge(returnAge) {

var dob_month;
var dob_day;
var dob_year;
var dob;

dob = document.form1.elements.dob.value;

var birthDay = dob.split("-");

dob_month = birthDay[0];
dob_day = birthDay[1];
dob_year = birthDay[2];

var time=new Date();
var lmonth=time.getMonth() + 1;
var date=time.getDate();
var year=time.getYear() + 1900;

var returnAge = year - dob_year;

document.form1.r_age.value=returnAge

alert("Your age is: " + returnAge);

}
</script>

You can change the size, or name, or whatever you want to in the textbox. I was just doin' that to show you what you could do.

tdavis
11-23-2005, 10:16 PM
Thanks. But there is one little problem. The value is returned correctly, but displays and then disappears. Do I need to change the attributes in the script?

boxxer03
11-23-2005, 10:21 PM
trying switching the alert and the document.form1.r_age.value=returnAge. put the alert before the doucument one.

Brandoe85
11-23-2005, 10:22 PM
Couple things I see. You have a parameter in your function that you're never using, which also has the same name as a variable you're declaring in your function. I think if you put a span or div tag in your page and passed the form back to your function, you could set the innerHTML of your div to the calculated age. You might want to change the input type to a button instead of submit, as it will clear everything out after the button is clicked.

Good luck;

tdavis
11-23-2005, 10:31 PM
OK. I changed the submit to a button, and the answer does not disappear. So everything works great now. Hopefully, I can use this in the real form.

Thanks to ALL very much for your help!
-tdavis

tdavis
11-23-2005, 10:51 PM
Well, it sort of works. The age seems to be calculated properly, but does not populate the AGE form field until the second time I try and specify a date. I do not actually enter the date, I am using another function for that.

Is there an issue using two functions here?

<td><b><input class="input" type="text" name="the_date" size="18" maxlength="20" class="input" onFocus="hideAll();showAndFocus('dateDiv',document.date_form.the_month);calcAge()"></b></td>

The page can be seen here:
http://www.tdavisconsulting.com/kenerly

Place the cursor on date of birth, and a drop down displays to enter dob.

thanks again!
-tdavis

PhotoJoe47
11-24-2005, 12:09 AM
Your orginal question was can a function return a value. I showed you how it was done using the keyword return. Now you take the variable that you use to store the dob that the user input for the field you have label "Birthday:*". Now when you want to display the age you can use:

document.write(calcAge(dob);

Try it.

PhotoJoe47
11-24-2005, 01:45 AM
I also notice one other little detail you might want to look at.

With this bit of code:

var returnAge = year - dob_year;

if the current date is before the user's birthday the age will be off by a year to much. So you will need to check and see if the current date (month and day) is greater than the user's birthday (month & year)

boxxer03
11-24-2005, 06:38 AM
can you post the whole new code you are you using now?

boxxer03
11-24-2005, 07:15 AM
I took the original code and changed it a little and this works perfect. The only problem is like PhotoJoe said, is that say you were born in 1984 then any month or day you use for this year will return 21 even if you haven't had your b-day yet. You need to add a couple lines that pretty much checks to see if the month/day they entered is before or on this day and if it isn't then subtract 1.

<script type="text/javascript">
<!--
function calcAge() {

var dob_month;
var dob_day;
var dob_year;
var dob;

dob = document.form1.elements.dob.value;

var birthDay = dob.split("-");

dob_month = birthDay[0];
dob_day = birthDay[1];
dob_year = birthDay[2];

var time=new Date();
var lmonth=time.getMonth() + 1;
var date=time.getDate();
var year=time.getFullYear();

var returnAge = year - dob_year;

document.form1.r_age.value=returnAge

alert("Your age is: " + returnAge);

}
//-->
</script>


<body>

<form name="form1">

<p>Enter date of birth in format MM-DD-YYYY
<input id="dob" type="text" name="dob" value="">
</p>

<p id="itext">
Click the button below to calculate your age
</p>

<p>
<input type="button" value="Calculate" onclick="calcAge()">
</p>

<p>
Your age is: <input type="text" name="r_age" size="10">
</p>
</form>

tdavis
11-24-2005, 02:15 PM
I did not have any considerations of month and day before, but always intended to put that in. Here is the "test code". I have it also in an actual page that I am putting together at http://www.tdavisconsulting.com/kenerly. The test code works, but the actual page does not. I am not sure where to put the calcAge(). I have it on the DOB along with the date selector process (with onFocus). It works, but the date does not get put into the AGE field until the 2nd time.

Maybe I should use onBlur?

<script type="text/javascript">
function calcAge(returnAge) {

var dob_month;
var dob_day;
var dob_year;
var dob;
var message;

dob = document.form1.elements.dob.value;

var birthDay = dob.split("-");

dob_month = birthDay[0];
dob_day = birthDay[1];
dob_year = birthDay[2];

var time=new Date();
var lmonth=time.getMonth() + 1;
var date=time.getDate();
var year=time.getYear() + 1900;

returnAge = year - dob_year;

if (dob_month > lmonth) {
returnAge--;
}

if (dob_month == lmonth && dob_day > date) {
returnAge--;
}

document.form1.r_age.value=returnAge
return returnAge;

}
</script>


<body>

<form name="form1">

<p>Enter date of birth in format MM-DD-YYYY
<input id="dob" type="text" name="dob" value="">
</p>

<p id="itext">
Click the button below to calculate your age
</p>

<p>
<input type=button value='Click here to calculate your age' onclick="calcAge()">
</p>

<p>
Your age is: <input type="text" name="r_age" size="10" readonly style="border:0px solid black; background-color:transparent;">
</p>

</form>

Kor
11-24-2005, 04:30 PM
use getFullYear() instead of getYear()+1900

tdavis
11-24-2005, 05:11 PM
Thanks. I will change to using getFullYear(), but I dont think that will resolve my most pressing issue right now. I have my test code working, but the actual form has a problem.

I dont know where to put the calcAge() function call.
Does anyone have any suggestions?

Thanks!

Kor
11-24-2005, 05:58 PM
Here it is:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta http-equiv="Content-Style-Type" content="text/css">
<meta http-equiv="Content-Script-Type" content="text/javascript">
<script type="text/javascript">
var today = new Date();
var ty = today.getFullYear();
var tm = today.getMonth();
var td =today.getDate();
function calcAge(){
var inp = document.getElementById('dob').value.split('-');
var dif = ty-Number(inp[2]);
dif=((tm<inp[0])||(tm==Number(inp[0])&&td<Number(inp[1])))?dif-1:dif;
document.getElementById('ag').firstChild.data=dif;
}
</script>
</head>
<body>
<p>Enter date of birth in format MM-DD-YYYY
<input id="dob" type="text" name="dob" value="">
</p>

<p id="itext">
Click the button below to calculate your age
</p>

<p>
<input type=submit onclick="calcAge()">
</p>

<p>
Your age is:<span id="ag">&nbsp;</span>

</body>
</html>

PhotoJoe47
11-24-2005, 08:26 PM
Hi Kor,

It seems a lot of people are working the date object these days.

I still have a hard time understanding the conditional statement. So once again would you mind explainging this line of code?

dif=((tm<inp[0])||(tm==Number(inp[0])&&td<Number(inp[1])))?dif-1:dif;

I have not seen the Number() function before, what is it's syntax?

PhotoJoe47
11-24-2005, 09:04 PM
Ok,
I when back and redid the line of code like this. Now it makes a little more sense.
dif= ((tm<inp[0]) || (tm == Number(inp[0])&& td<Number(inp[1])))? dif-1 : dif;

Lets see if I can explain it and you let me know if I'm right.

dif is the number of years between the two date objects. The conditional statement will check if the value of dif needs to be change by -1 or not.
It will subtract 1 if today's month is less than the input month.
Or it will subtract 1 if today's month is equal to the input month and today's day of the month is less than the input's day of the month.

I'm guessing that the Number() is just making sure that a number is being compared?

How did I do?

Well it is time to go eat some turkey.

Happy Thanksgiving to all.

liorean
11-24-2005, 09:21 PM
The basic JavaScript types each have a global object like that.

It's a way to convert other objects to numbers. Here's a table you can see how it behaves in:n Number(n) parseInt(n) parseInt(n,10) parseFloat(n) isNaN(n) eval(n)
---- ---- ---- ---- ---- ---- ----
'8' 8 8 8 8 false 8
'.8' 0.8 NaN NaN 0.8 false 0.8
'-8' -8 -8 -8 -8 false -8
'08' 8 0 8 8 false 8
'1e3' 1000 1 1 1000 false 1000
'1d3' NaN 1 1 1 true ERROR
'0x1d3' 467 467 0 0 false 467
'd' NaN NaN NaN NaN true ERRORNumber(n) behaves exactly the same as the numeric operator shortcuts we sometimes use: +n, 1*n, n-0, n/1

Kor
11-25-2005, 10:06 AM
dif is the number of years between the two date objects. The conditional statement will check if the value of dif needs to be change by -1 or not.
It will subtract 1 if today's month is less than the input month.
Or it will subtract 1 if today's month is equal to the input month and today's day of the month is less than the input's day of the month.

Exactly. About the Number() liorean had a comrehensive answer above.

It seems a lot of people are working the date object these days.

There must be a Date festival or contest somewere...:D


 

TOP

Hyperthreading hurts s
HP sharpens blade PC l
Apple goes for Quad po
Itanium gets scaled do

For more info

Hyperthreading hurts s
HP sharpens blade PC l
problem with an ImageP
Modifying a navigation
convert text to intege
Anyone knows to clear 
Automatically generate
document.getElementByI
Hi question about stor
Netscape Problems :-( 

News Archive

Month and Year drop do
navigation button/imag
doubt in window.open()
undo redo script 
Mimick "View Source" w
print Question 
No-image representatio
What's wrong with this
Urgent help in Javascr
Gurt Menu assistance 

Related stories:

Anyone knows to clear the contents of the window !!! in javascript
Automatically generates color tags(<color="red">,</color>)
document.getElementByID() Issues
Adding onclick stuff to an INPUT through javascript
Netscape Problems :-(
problem with an ImagePreview js
do I need a random script script or not?

Copyright@2004-2005 www.zzcoke.com All Right Reserved

advanced web statistics