473,545 Members | 2,444 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Leap Year dertermination

25 New Member
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="JAVAS CRIPT>
<!-- Hide from old browsers

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

if (flag == 0) :
window.alert("T he year you have entered is a leap year.")
else:
window.alert("T he 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="leapYe ar()">
</form>
</body>[/HTML]
Jun 16 '07 #1
12 7656
jsakalos
12 New Member
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
Brigitte Behrmann
25 New Member
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 Recognized Expert Moderator Expert
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("T he year you have entered is a leap year.");
} else {
window.alert("T he 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="leapYe ar(document.get ElementById('ye ar').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 Recognized Expert Moderator Expert
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
jsakalos
12 New Member
Can anything be simpler than this?

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

[php]
var isLeap = function(year) {
return new Date('Feb 29, ' + year).getMonth( ) === 1;
};
// usage
alert(isLeap(20 00));
alert(isLeap(20 01));
[/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
jsakalos
12 New Member
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
Brigitte Behrmann
25 New Member
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 Recognized Expert Moderator Expert
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

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

Similar topics

14
4745
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 namespace std; class Year { public:
8
2956
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 program will make the decision if it is a leap year or not. In addition, I kind of understand that there are certain rules to calculating leap...
3
3639
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 = year % 4 if result > 0: print "It is NOT a leap year" else: print "It IS a leap year"
7
3534
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 code...if you can help i'd gladly appreciate it. int year, test4, test100, test400; printf("The amazing leap year teller thingie...
8
3895
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 case they are leap years. Write a script that allows a user to enter a year and then determine whether the year entered is a leap year. Include a...
37
14905
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: case 8: case 11: case 14: case 17: return 1; default: return 0;
5
3477
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
9061
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 should then calculate whether or not the year is a leap year. The script should caculate whether or not it is a leap year using the following algorithm. ...
4
4412
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 friends with a birthday inside the next 3 days, taking into account leap years for some1 born on the 29th of feb to be born on 28th in non leap years...
0
7490
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7425
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7935
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7449
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6009
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5351
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5069
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3465
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
734
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.