-1

This is my code so far:

/*function isSchrikkeljaar(year) {
    year = 2016;
    if (//is empty(year)) {
       //use current year
    }
    if (year%4===0 && (year%100!=0 || year%400===0)) {
       return true;
       console.log(true);
    } else {
       return false;
        console.log(false);
    }
}*/

If the year is a leap year it logs true, if not it should log false, if the year is empty it should take the current year. Can anyone help me out, been stuck for a while. The problem is that I don't know how to give it the current year when the year is empty.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Maxim
  • 631
  • 5
  • 10
  • Remove the year=2016. do year=year||2016 , this will take 2016 if isSchrikkeljaar – Jonas Wilms Jan 11 '17 at 14:50
  • If the logs are not showing, move the `log` statement above `return` otherwise those will not execute. – Tushar Jan 11 '17 at 14:52
  • 1
    Also a duplicate of [Set a default parameter value for a JavaScript function](http://stackoverflow.com/q/894860/218196) ... please use the search before you ask a new question. – Felix Kling Jan 11 '17 at 14:56
  • [`var isLeapYear = (year = new Date().getFullYear()) => year % 4 === 0 && (year % 100 !== 0 || year % 400 === 0);`](https://jsfiddle.net/tusharj/3gkur20j/) – Tushar Jan 11 '17 at 14:58

1 Answers1

1
 function isSchrikkeljaar(year) { 
    year = year||new Date().getFullYear();
    if (year%4===0 && (year%100!=0 || year%400===0)) { 
       return true; 
    } else { 
       return false;
    } 
 }

If year is set take year as year, if not take the actual date and get its year.

Use cases:

isSchrikkelyear();//2017 = false
isSchrikkelyear(2016); //false
Jonas Wilms
  • 132,000
  • 20
  • 149
  • 151