Closed Thread Icon

Topic awaiting preservation: Why does this work??? Very Strange to me! (Page 1 of 1) Pages that link to <a href="https://ozoneasylum.com/backlink?for=8659" title="Pages that link to Topic awaiting preservation: Why does this work??? Very Strange to me! (Page 1 of 1)" rel="nofollow" >Topic awaiting preservation: Why does this work??? Very Strange to me! <span class="small">(Page 1 of 1)</span>\

 
Hiroki
Paranoid (IV) Inmate

From: NZ
Insane since: Dec 2002

posted posted 05-18-2003 23:38

Hi, guys. How are you today? Well, could you share your time with me, please?
I am coding program, which calculate user's age according to the input ( the Year of Birth). Here is code. My question is that how JS recognize the argument YearBirth is value of text1. Value of text1 is the name of first text box.

code:
<html>
<head>
<title>Age Calculator</title>

<script type="text/javascript">
function CalculateAge(YearBirth)
{
var thisYear=2003;
return (thisYear-YearBirth);
}
</script>
</head>

<body>
<form name="form1">
<p><b>Enter in the top text box the year you were born</b></p>
<input type="text" name="text1" size="5">
<input type="button" name="submit" value="Show My Age"

onClick="form1.text2.value=CalculateAge(form1.text1.value)">
<input type="reset" value="Reset">
<p>This year you will trun:
<input type="text" name="text2" size="3"></p>
</form>

</body>
</html>



Many many thanks.


Hiroki Kozai

InI
Paranoid (IV) Mad Scientist

From: Somewhere over the rainbow
Insane since: Mar 2001

posted posted 05-18-2003 23:56

The poster has demanded we remove all his contributions, less he takes legal action.
We have done so.
Now Tyberius Prime expects him to start complaining that we removed his 'free speech' since this message will replace all of his posts, past and future.
Don't follow his example - seek real life help first.

Hiroki
Paranoid (IV) Inmate

From: NZ
Insane since: Dec 2002

posted posted 05-19-2003 00:35

I have to admit this is the case I always worry about. Sorry to make you have hard time.

Well, this code come from my tutor. I am trying to understand his code and modifying it myself.

What I don't know is about "argument" called YearBirth. Actually how does augument works???

What I am coding is very simple. If user enter his/her age, JS shows her birth year.
When I see my totor's code, it make sense to me except argument or return. In the head section, it said

return (thisYear-YearBirth);

This code calculate the user's birth year, right? But how JS knows YearBirth is the value of form, named text1??? I cannot find any relationship between YearBirth and text1.

Hmm....I cannot express more.
Sorry my Poor English........


Hiroki Kozai

Veneficuz
Paranoid (IV) Inmate

From: A graveyard of dreams
Insane since: Mar 2001

posted posted 05-19-2003 00:55

The magic lies in these lines:

code:
<input type="button" name="submit" value="Show My Age" 

onClick="form1.text2.value=CalculateAge(form1.text1.value)">


This line tells the browser that when the 'submit' button is pressed the value of the form element named text2 in the form named form1 will become the value that the CalculateAge function returns. The CalculateAge function knows what value to use as YearBirth since you pass that value along in the function call by using form1.text1.value as the argument. The way to define arguments in a function is to place the argument name in a paranteses behind the function when you create it.

code:
function CalculateAge(YearBirth)
{
var thisYear=2003;
return (thisYear-YearBirth);
}
</script>


On the first line here you see that the function CalculateAge has a parantese with YearBirth inside it. This tells the function that the YearBirth variable will get the value of whatever is inside the parantese when the function is called. In your case the function is called like this: CalculateAge(form1.text1.value) so the YearBirth gets the value of text1.

Hope that helps to clarify it If not you could always ask your tutor, he should be able to explain it.

_________________________
"There are 10 kinds of people; those who know binary, those who don't and those who start counting at zero"

[This message has been edited by Veneficuz (edited 05-19-2003).]

Hiroki
Paranoid (IV) Inmate

From: NZ
Insane since: Dec 2002

posted posted 05-19-2003 04:53

Hi, Veneficuz!
Many many thanks for your explanation.
I have to consider of it but it clears me very much.
I really thank you.
Cya.

Hiroki Kozai

« BackwardsOnwards »

Show Forum Drop Down Menu