473,385 Members | 1,780 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Leap Year dertermination

Can anyone assist with my code. I just cannot get this to work:
I have to write a script that allows the user to enter a year & then determine whether it is a leap year or not. The request is for a form with a single text box and using an alert dialog box stating if the entered year is a standard or leap year.

Code so far:
[HTML]<HTML>
<HEAD>
<TITLE>Is it a leap year?</TITLE>
</HEAD>

<SCRIPT LANGUAGE="JAVASCRIPT>
<!-- Hide from old browsers

function leapYear(){
Last2Digits = year % 100
if (Last2Digits == 0):
flag = year % 400
else:
flag = year % 4

if (flag == 0) :
window.alert("The year you have entered is a leap year.")
else:
window.alert("The year you have entered is a standard year.")
}

//-->
</script>
</head>
<body>
<form><p>Year: <input type="text" name="Year"></p>
<input type="button" value="Check for Leap Year" name="check for leap year" onclick="leapYear()">
</form>
</body>[/HTML]
Jun 16 '07 #1
12 7622
There is one trick you could use with Date object. First set date to "Feb 29, YYYY" and then get month from the same Date object. If you get Feb it's leap year, if you get March it's not.

Beware, months are numbered from 0 - 11.
Jun 16 '07 #2
Nope, you've lost me there... anyone else got some ideas for a simple fix. The question is only worth 15 marks, but with the amount of time I have spent on this it shoud have been 150!!!

Expand|Select|Wrap|Line Numbers
  1. <HTML>
  2. <HEAD>
  3. <TITLE>Is it a leap year?</TITLE>
  4. </HEAD>
  5.  
  6. <SCRIPT LANGUAGE="JAVASCRIPT>
  7. <!-- Hide from old browsers
  8.  
  9. function leapYear(){
  10.    Last2Digits = year % 100
  11.    if (Last2Digits == 0):
  12.        flag = year % 400
  13.    else:
  14.     flag = year % 4
  15.  
  16.    if (flag == 0) :
  17.        window.alert("The year you have entered is a leap year.")
  18.    else:
  19.        window.alert("The year you have entered is a standard year.")
  20. }
  21.  
  22. //-->
  23. </script>
  24. </head>
  25. <body>
  26. <form><p>Year: <input type="text" name="Year"></p>
  27. <input type="button" value="Check for Leap Year" name="check for leap year" onclick="leapYear()">
  28. </form>
  29. </body>
  30.  
Jun 17 '07 #3
gits
5,390 Expert Mod 4TB
the following works your way:

[HTML]<title>Is it a leap year?</title>
<head>
<script>
function leapYear(year) {
var Last2Digits = year % 100;
if (Last2Digits == 0) {
flag = year % 400;
} else {
flag = year % 4;
}

if (flag == 0) {
window.alert("The year you have entered is a leap year.");
} else {
window.alert("The year you have entered is a standard year.");
}
}
</script>
</head>
<body>
<form>
<p>Year:
<input type="text" id="year" name="Year">
</p>
<input type="button" value="Check for Leap Year" name="check_for_leap_year" onclick="leapYear(document.getElementById('year'). value);">
</form>
</body>
[/HTML]

please compare it to your not-working code ... have a look at your head-tags, correct your if-statements, pass the year to your function until it expects year to work with ...

the errors you make are really simple ... and i would recommend that you double-check the syntax and the used values and variables ... try!!! to use firefox and the firebug-extension that shows you instantly on testing, whats wrong with your code ...

kind regards ...
Jun 17 '07 #4
gits
5,390 Expert Mod 4TB
in case you wanted to use the conditional operator use it the following way:

Expand|Select|Wrap|Line Numbers
  1. var flag = Last2Digits == 0 ? year % 400 : year % 4;
  2.  
this usually is used for assigning a value to a variable based on a condition.

kind regards ...
Jun 17 '07 #5
Can anything be simpler than this?

[php]
var isLeap = function(year) {
return new Date('Feb 29, ' + year).getMonth() === 1;
};
// usage
alert(isLeap(2000));
alert(isLeap(2001));
[/php]
Jun 17 '07 #6
gits
5,390 Expert Mod 4TB
Can anything be simpler than this?

[php]
var isLeap = function(year) {
return new Date('Feb 29, ' + year).getMonth() === 1;
};
// usage
alert(isLeap(2000));
alert(isLeap(2001));
[/php]
you're right ... that's a slick way to achieve the goal. but let us try to help her with the code she has already written ... until she starts with javascript it is an good idea to let her code and correct the errors ... so that she gets the feeling and the ideas of what to do ... but i agree ... your code is real cool ;)

kind regards ...
Jun 17 '07 #7
OK,

let's let her decide. You see, I'm a kind of guy that looks for solutions. So I wouldn't insist on my 100 lines of code if I had a 3 lines replacement.

Nevertheless, It's good to learn to make those 100 lines bugfree. :)
Jun 17 '07 #8
OK,

let's let her decide. You see, I'm a kind of guy that looks for solutions. So I wouldn't insist on my 100 lines of code if I had a 3 lines replacement.

Nevertheless, It's good to learn to make those 100 lines bugfree. :)
Hi Guys
I am in no position to argue. At the end of the day I have an assignment that I have to submit using JavaScript and it has to be posted off tomorrow, so whatever you can help me with will be great. I have printed all your responses and filed to keep as reference with my studies, but if I do not submit the assignments I will not get sufficient credits to even qualify to write the exam at the end of the year... so PLEASE HELP... whatever you got as long as long as it is JS code I'm cool!
Jun 17 '07 #9
gits
5,390 Expert Mod 4TB
of course and i fully agree with that ... but one thing too: there are no 100 lines ;) ... and of course it is her decision ... and nobody insists ;) but: she used the if-statement incorrect ... and with showing her an entire new solution ... we wouldn't help her to learn the correct usage ... do you know what i mean? ...

@brigitte: the solution that jsakalos showed is for some reasons better then ours ;) ... the main reason is: it is shorter ... and typically in real-world projects we try to write as little code as we can do ... this is for maintainance reasons in case of adaptions as well as for errors ... more lines of code may produce more errors ... ok? ...

kind regards ...
Jun 17 '07 #10
of course and i fully agree with that ... but one thing too: there are no 100 lines ;) ... and of course it is her decision ... and nobody insists ;) but: she used the if-statement incorrect ... and with showing her an entire new solution ... we wouldn't help her to learn the correct usage ... do you know what i mean? ...

@brigitte: the solution that jsakalos showed is for some reasons better then ours ;) ... the main reason is: it is shorter ... and typically in real-world projects we try to write as little code as we can do ... this is for maintainance reasons in case of adaptions as well as for errors ... more lines of code may produce more errors ... ok? ...

kind regards ...
Agreed, I will give it a try after I have finished making dinner.... which is probably burning in the oven as we speak.... watch this space in case I get stuck!

Thanks - BB
Jun 17 '07 #11
of course and i fully agree with that ... but one thing too: there are no 100 lines ;) ... and of course it is her decision ... and nobody insists ;) but: she used the if-statement incorrect ... and with showing her an entire new solution ... we wouldn't help her to learn the correct usage ... do you know what i mean? ...
kind regards ...

Come on gits,

the last thing I want is to argue... And first thing I want is to help (and get helped when I need it).

PS: 100 was an example; just because it's nice number. I'm too lazy to count Brigitte's lines. ;)
Jun 17 '07 #12
gits
5,390 Expert Mod 4TB
ok ;) ... if you insist ... there are 13 of her lines against 3 of yours ... and as i said ... yours are better ... but there is nothing to argue that your solution doesn't show her the correct usage of an if-statement ... that is a basic construct in every programming language and should be corrected ...

you helped a lot with your solution ... it shows that often there is a much better solution to do things ... you are right with what you said ... and i think she could solve her problem now ... good job!

kind regards ...
Jun 17 '07 #13

Sign in to post your reply or Sign up for a free account.

Similar topics

14
by: Protoman | last post by:
Hi!!! I need some help on a project I'm that calculates leap years; I'm getting errors that I have no idea what they mean; here's the code: #include <iostream> #include <cstdlib> using...
8
by: rn216_ccc | last post by:
Hi there all, I found a web site,. http://www.onlineconversion.com/leapyear.htm, where it can give a list of leap years by within the given range by the user, or the user may enter a year and the...
3
by: xGx | last post by:
Hi please could someone help. i have created this programming so you can work out if there is a LEAP YEAR. But i need it to handle centuries. heres the code def isLeapYear (year): result =...
7
by: karafire2003 | last post by:
Hey there...i was wondering if someone can help me with my program. I'm doing the leap year program thing. i thought i had it all figured out, but it ALWAYS says that it's not a leap year. here's my...
8
by: SONIQ | last post by:
You can determine whether a year is a leap year by testing if it is divisible by 4. However, years that are also divisible by 100 are not leap years, unless they are also divisible by 400, in which...
37
by: mazwolfe | last post by:
I'm new here, so excuse me if my style is incorrect. Can anyone come up with a better method for this calculation? Code: int is_leap(int year) { switch (year % 19) { case 0: case 3: case 6:...
5
by: jimix | last post by:
here's what i have. using microsoft studio #define _CRT_SECURE_NO_DEPRECATE #include <stdio.h> int main( void ) { int year, b, c, e; b = 4; c = 100;
4
by: jrw133 | last post by:
Hello. So im a little bit stuck on one of my homework questions. heres the question: Write a C Shell Script that performs the following functions. Asks the user to input a year. The script...
4
by: hallsers | last post by:
Here is the problem i am having: - When a user logs in, on page load it will take the dob of all users in a stored friends list and compare them against the following conditions - To display any...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.