0

Howdy I've only just started working with basic JavaScript.

Write a program which will accept a year from the user and display whether the year is a leap year or not.

The teacher has not been much help and I don't know how I should go about the coding, I'm not asking for an answer - I would like a link or some referencing I could use to learn how to do this as I am pretty darn puzzled.

Below is an example of what I think it sort of needs to look like, I've only been doing JS for a couple days now so please if you're going to burn me remember that.

var year=prompt("Please input a year: ");

var remainder=year % 4
    if remainder === 4
        then 
document.write("It is not a leap year.");
        else
document.write("It is a leap year.");


document.write("You entered: " ,year, ".");
Irvin Dominin
  • 30,819
  • 9
  • 77
  • 111
  • 6
    JavaScript has no `then`. Look at the syntax at [MDN if...else](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/if...else) – epascarello Mar 18 '14 at 12:56
  • *if year is divisible by 400 then leap year else if year is divisible by 100 then common year else if year is divisible by 4 then leap year else common year* - http://en.wikipedia.org/wiki/Leap_years#Algorithm – deceze Mar 18 '14 at 12:59
  • I feel that since you're saying "I'm not asking for an answer", your question doesn't quite fit well here on StackOverflow, where the rule is "You should only ask practical, answerable questions based on actual problems that you face". – yoniLavi Apr 24 '17 at 00:18

2 Answers2

1

Some pointers:

  • prompt returns a string, not a number. Use parseInt(year, 10) to turn it into a number.
  • if statements look like if (condition) { thendothis(); } else { dothis(); }
  • The remainder of dividing by 4 cannot be 4, by definition
  • A year is a leap year IF: 1) It's divisible by 4 and 2) It's not divisible by 100 unless 3) it's divisible by 400. 2000 was a leap year, 2100 won't be. You should probably get it working for simple division by 4 first, then add the extra rules.
RemcoGerlich
  • 30,470
  • 6
  • 61
  • 79
1

If you are mean to solve about leap, you may refer to Check if year is leap year in javascript

Or

You may also try to refer to Learn How To Use JavaScript Dates and Leap Years.

Apart from reading online resources like Javascript in MDN To grab a good understanding of Javascript, you may read the free ebook Eloquent JavaScript by Marijn Haverbeke

This should be a good starting for you. A good basic will save your lots of time. Good luck.

Community
  • 1
  • 1
  • cheers all, I guess I'll go back through some of the basics first with similar coding (can't say I've used parseInt before) - going to be a beautifully long night :) – user3433144 Mar 18 '14 at 13:16