|
|
Hiroki
Paranoid (IV) Inmate
From: NZ Insane since: Dec 2002
|
posted 05-27-2003 04:27
Hi, guys. I am back to work now.
I had korean fried rice. I made it last night.
Not as good as I thought. A bit pitty.
Never mind.
Well, would you mind seeing my code, please.
I am working on next mission.
My mission:
quote: I am writing a program to check to see if an entered year is a leap year. A year is a leap year if it is divisible by 4, except when it is a turn of the century year. A turn of the century year is only a leap year if it is divisible by 400. Place whether the year is a leap year or not into a text box on the page.
Here is my code:
code:
<html>
<head>
<script type="text/javascript">
function cal()
{
var theYear = document.theForm.year.value;
var theResult = document.theForm.result.value;
if (theYear/400=0)
{
theResult = document.write("Century!");
}
else if ( theYear/4 = 0)
{
theResult = document.write("Leap Year!");
}
else
{
theResult = document.write("Ordinary Year!");
}
document.theForm.year.value = theResult;
}
</script>
</head>
<body >
<form name="theForm">
<p>
Enter the Year:
<input type="text" name="year">
</p>
<p>
<input type="button" value="Get result" onClick="cal()">
<input type="reset" value="Reset">
</p>
<p>
This year is:
<input type="text" name="result">
</p>
</form>
</body>
</html>
I made sure if I had put semi colon and else if state mement.
But something wrong.....Doesn't work.
Would you see anything????
Many thanks for reading this.
Hiroki Kozai
|
Dracusis
Maniac (V) Inmate
From: Brisbane, Australia Insane since: Apr 2001
|
posted 05-27-2003 08:21
When your testing for equality in an if() statement you need to use "==". The single equals sign "=" is an assignment operator, meaning it'll assign x vale to y like y = x. The double equals sign "==" is a comparison operator that will test for equality. Hope that makes sense.
Try if ((theYear/400) == 0) or simply, if (!(theYear/400))
I don't know weather or not the extra brackets are needed. I tend to go a little overboard in this area but I always figure it's better to have too many than to have too little to the point that your code won't execute properly.
|
quisja
Paranoid (IV) Inmate
From: everywhere Insane since: Jun 2002
|
posted 05-27-2003 10:15
Is checking if when divided it equals 0 the right thing to do? For example the year 2004 is a leap year, I think, but dividing this by 4 will not equal zero. What you need to be doing is checking if the remainder equals 0 when yuou do integer division on it. However, I don't know the JavaScript function or operator for this...
|
bitdamaged
Maniac (V) Mad Scientist
From: 100101010011 <-- right about here Insane since: Mar 2000
|
posted 05-27-2003 10:29
quisja is on the right track (well way most programmers use ). The operator you want is the modulus operator "%" which will return the remainder of the division operation. So if you do:
if (!(theYear%400))
which means if theYear/400 has a remainder of 0
.:[ Never resist a perfect moment ]:.
|
Hiroki
Paranoid (IV) Inmate
From: NZ Insane since: Dec 2002
|
posted 05-27-2003 23:44
Hi, guys.
Many many thanks for your replies.
I really appreciate that.
Well, I realized that my code was logically wrong when I was in the bed last night.
Then I encountered same question with you, quisja.
And many many thanks for giving me an answer for it, bitdamaged.
Really thank you and be gald to know that.
This morning I fixed my code like you told me, guys.
Then I got another problem. It is sort of running.
But when I click button, always go to another page not showing in text box I intended.
What I wanted is that if user calculate the year, text appears in text box underneath the calculate button.
And one more thing.....Do I need document.theForm.result.value = theResult; at the last part of function??? Hmm....it seems causing error.....Would you see something???
Here is my code:
code:
<html>
<head>
<script type="text/javascript">
function cal()
{
var theYear = document.theForm.year.value;
var theResult = document.theForm.result.value;
if (!(theYear%400))
{
theResult = document.write("Century!");
}
else if (!(theYear%4))
{
theResult = document.write("Leap Year!");
}
else
{
theResult = document.write("Ordinary Year!");
}
document.theForm.result.value = theResult;
}
</script>
</head>
<body >
<form name="theForm">
<p>
Enter the Year:
<input type="text" name="year">
</p>
<p>
<input type="button" value="Get result" onClick="cal()">
<input type="reset" value="Reset">
</p>
<p>
This year is:
<input type="text" name="result">
</p>
</form>
</body>
</html>
My uploaded code is here.
Ps. Error message said: document.theForm.resu is not an object.
many many thanks for reading this.
Have a happy Wendnesday.
Ps. I have database test tomorrow.
Hmm....heaps things has to be done....
Here is my code:
Hiroki Kozai
|
bitdamaged
Maniac (V) Mad Scientist
From: 100101010011 <-- right about here Insane since: Mar 2000
|
posted 05-28-2003 00:27
yup pushing buttons usually causes something to happen. Since you are kind of hijacking the process you need to kill the default action for the button.
At the end of your "cal()" function put
return false;
and then where you call it on your onClick put
onCLick="return cal()"
and that should kill the normal button actions
.:[ Never resist a perfect moment ]:.
|
Hiroki
Paranoid (IV) Inmate
From: NZ Insane since: Dec 2002
|
posted 05-28-2003 00:48
Hi, Bitdamaged.
Many many thanks for your reply.
Well, I changed it like you said.
Would you mind explaining a bit more, please? What do you mean that kill the button??? And what does return false; do???
And one more thing, please. Still those words ( Leap, century or ordinary year) doesn't appear in the box but on new page.
Would you see why???
code:
<html>
<head>
<script type="text/javascript">
function cal()
{
var theYear = document.theForm.year.value;
var theResult = document.theForm.result.value;
if (!(theYear%400))
{
theResult = document.write("Century!");
}
else if (!(theYear%4))
{
theResult = document.write("Leap Year!");
}
else
{
theResult = document.write("Ordinary Year!");
}
return false;
}
</script>
</head>
<body >
<form name="theForm">
<p>
Enter the Year:
<input type="text" name="year">
</p>
<p>
<input type="button" value="Get result" onClick="return cal()">
<input type="reset" value="Reset">
</p>
<p>
This year is:
<input type="text" name="result">
</p>
</form>
</body>
</html>
Hiroki Kozai
|
Petskull
Maniac (V) Mad Scientist
From: 127 Halcyon Road, Marenia, Atlantis Insane since: Aug 2000
|
posted 05-28-2003 14:23
return false; is kinda simple, really..
it cancells what would *normally* be done in regular operation..
For example, if you want your neato script to run like: onSubmit="myFunction();" then when you submit your form your function will run and after your function runs the form will still sumit itself...
what you want a something that tells the script, "Hey!! I already took care of this action myself!! Don't do what you're *supposed* to do!"
.... and that's what 'return false;' does... you type this: onSubmit="myFunction();return false;", and you function will run before the script submits- and when it comes time for the form to submit it simply won't.
did any of that make sense? because I'm not sure I explained it well...
Code - CGI - links - DHTML - Javascript - Perl - programming - Magic - http://www.twistedport.com
ICQ: 67751342
|
RoyW
Bipolar (III) Inmate
From: Insane since: Aug 2001
|
posted 05-28-2003 16:25
Even if the onclick worked you have a flaw in your overall logic.
A century is 100 years. Therefore your logic needs to divide the year by 4, 100 and 400 to find the correct results.
1900 not a leap year (century)
1904 is a leap year (normal)
2000 is a leap year (century)
2004 is a leap year (normal)
|
Clay
Nervous Wreck (II) Inmate
From: Utreg, NL Insane since: Nov 2002
|
posted 05-28-2003 21:27
You could also just use the built in Date object. If you feed a month 32 days (and it has 30) the date will move on to the 2nd day of the next month. So, if you set the given year to the 29th of february, and it returns march, it's not a leap year.
code:
function checkLeap(year) {
var date = new Date(year,1,29);
if(date.getMonth() == 1) {
alert(year + ': leap');
} else alert(year + ': nope');
}
checkLeap(1996)
checkLeap(1997)
checkLeap(2024)
peterned
[This message has been edited by Clay (edited 05-28-2003).]
|
Hiroki
Paranoid (IV) Inmate
From: NZ Insane since: Dec 2002
|
posted 05-29-2003 05:06
Hi, guys. Many thanks for your replies.
I am having database test in 15 minutes so that I will read and think about it later.
Many many thanks.
Cya.
Hiroki Kozai
|
Suho1004
Maniac (V) Inmate
From: Seoul, Korea Insane since: Apr 2002
|
posted 05-29-2003 09:25
Not that I really have anything to contribute to the discussion, but I saw this:
quote: Hi, guys. I am back to work now.
I had korean fried rice. I made it last night.
Not as good as I thought. A bit pitty.
What does "pitty" mean?
And if you made it yourself, that might explain why it wasn't as good as you thought... I'll have to make some for you sometime.
|
wrayal
Bipolar (III) Inmate
From: Cranleigh, Surrey, England Insane since: May 2003
|
posted 06-02-2003 20:04
This is sort of a dead topic now, but I thought Id just make my suggestion. I havent gone through the logic of your code, but the problem with it coming up on a new page is this: You are writing theResult = document.write("Leap Year!"); . The document.write() causes it to be written on a new page. But, also, because it is on a new page, there is no longer a form.result. Taking this out would remove both problems.
But since someone else already suggested another, much shorter way, this isnt much use. But there you go, hopefully you wont make the document.write mistake in future. God knows I made it enough times while I was getting to grips with it...
Wrayal
Go to kimber-ja.demon.co.uk and click on the link to the raytracer! It's mine!
|
Hiroki
Paranoid (IV) Inmate
From: NZ Insane since: Dec 2002
|
posted 08-06-2003 01:45
Hi, Petskull:
quote: what you want a something that tells the script, "Hey!! I already took care of this action myself!! Don't do what you're *supposed* to do!"
It is great explanation to me. I am just redoing old JavaScript exercises.
I am re-reading this post again.
Lot of things are worth doing again and again until I get it, aren't they?
Here is my final uploaded file.
Many thanks for your helps, guys.
Cya.
Hiroki Kozai
|