Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Jeff Ward
8,978 PointsHelp with JavaScript Function: Print out the next 20 leap years
I have a question for school that is stumping me. I don't have an editor to run the JavaScript in, so I am using CodeAcademy and JSFiddle to run it. Both are saying "Un-expected 'else' found.
QUESTION: Write a function that takes ANY year given by user, and prints out the NEXT 20 leap years.
Can someone please look over my code and see if its correct?
Thanks!
var year = prompt("please input a year to find out the next 20 years");
var counter = 20;
var result=true;
function leapyear(year, counter, result) {
while (counter >= 0) {
if ((year % 4 === 0) && (year % 100 !== 0)||(year % 400 === 0)); {
result = true;
console.log(year);
year++;
counter--;
}
else {
year++;
}
}
}
3 Answers

Marcus Parsons
15,719 PointsHi Jeff,
As Jason mentioned, syntax wise your code works. But, there are a bunch of logic errors present such that, at the moment, your function doesn't do what you need it to. No worries, though, because I can help with that.
Take a look at all the comments I put! :)
var year = prompt("please input a year to find out the next 20 leap years");
var counter = 20;
//no need for a result variable
//can have two arguments, first is the year entered by user
//second is how many leap years to display
function leapyear(years, num) {
//convert years to integer
years = parseInt(years);
//cache reference to original number of leap years to be shown
//for use in return statement
var origNum = num;
//add 1 to years just in case current year is leap year
years++;
//this string will hold all of the leap years
var leapYears = '';
//while num is above 0
while (num > 0) {
//if years is a multiple of 4 and years is not evenly divisible by 100
//or years is evenly divisible by 100 and evenly divisible by 400
//it is a leap year
if (years % 4 === 0 && (years % 100 !== 0 || ( years % 100 === 0 && years % 400 === 0))) {
//if at the last year
if (num === 1) {
//add a period to end
leapYears += years + ".";
}
else {
//otherwise, add a comma and space for other years
leapYears += years + ", ";
}
//increase years
years++;
//decrease num
num--;
}
else {
//if not a multiple of 4, just add to years
years++;
}
}
//returns sentence after while loop is done
return "The next " + origNum + " leap years are " + leapYears;
}
//write to document the result of running leapyear
document.write(leapyear(year, counter));
//or
//alert result of running leapyear
//alert(leapyear(year, counter));
Jeff Ward
8,978 Pointsthanks this is EXTREMELY helpful!! You are awesome!!
The comments are VERY helpful :D
I was under the impression that leap years have to be: divisible by 4 divisible by 100 and divisible by 400
Where in your code does the program 'load up' the var leapYears?
Thanks!

Marcus Parsons
15,719 PointsAh, an oversight on my part that I will rectify. I'll comment as soon as I fix that. You had it almost correct in your if statement except that it needs to be all AND logic using &&! Also, the variable is loaded inside of the function where you see var leapYears = '';
. That is where it's created.
Jeff Ward
8,978 PointsThanks your a big help!

Marcus Parsons
15,719 PointsOkay it is done, and it is just a tad more complicated than I anticipated. I was wrong and you do need that OR logic in there but you also need another case of AND logic lol. I hope I explained it in the comment clear enough, and if not, then I'll go into further explanation :)
Jeff Ward
8,978 PointsYes you definitely did. That makes a lot of sense now. I can see how the code works and its great.
I love learning haha
Thanks for your help!!!!!!

Marcus Parsons
15,719 PointsAwesome! Glad to help, Jeff, and if you have any questions, I'll answer any that you have. Happy coding!
Jeff Ward
8,978 PointsI wish Treehouse had a friends list so I can add you, or private messages. I will just have to remember to refer back to this question and message you that way when I have a question in the future.
Thanks a ton!
Jeff
Jeff Ward
8,978 PointsI wish Treehouse had a friends list so I can add you, or private messages. I will just have to remember to refer back to this question and message you that way when I have a question in the future.
Thanks a ton!
Jeff

Marcus Parsons
15,719 PointsI'm actually going to be getting off Treehouse here soon. I'm at least pausing my membership so I can save money because I am currently in talks with a couple companies to get involved with a web development position. But, if you want, follow me on Twitter (you can find the link to that on my profile page on here), or send me a shoutout on my contact form and include your email and we can correspond that way. My contact form is on my site here: http://www.marcusparsons.com/contactme.php.
Jeff Ward
8,978 PointsBeautiful website and portfolio!! Very cool effects on them as well!!
Great thank you I appreciate it! I will send you an email in the future!!
Good luck at the job position! It certainly looks like you have the skills!!!
Are you 100% Treehouse 'self-taught'? Your very good.
Good luck and thanks again!

Marcus Parsons
15,719 PointsThank you! I appreciate that a lot! I put my blood, sweat (only cause it was hot in here), and tears (almost literally at times) into building that site and that portfolio. So many frustrating and then "Aha!" moments! haha
I've used a mixture of tools, Jeff. Most of the skills I've earned have come from helping others here on the forums and building projects. I'm not one of the types who can just watch a video and immediately absorb exactly everything the person is saying, but I like that you work on projects as you go through most courses. And then helping out others with those very same projects has helped to teach me more about certain concepts in JavaScript.
My advice is to daily build tiny JS projects that do random things. Look up everything in JavaScript if you're not 100% familiar with what that command/function/method/whatever does. The Mozilla Developer Network/MDN is an invaluable tool for anything dealing with pure JavaScript. If you use jQuery, use the jQuery API documentation to look up jQuery commands, although I would use Google/Your Favorite Search Engine and just look it up using "jquery nameOfCommandHere" because the jQuery search engine sucks lol.
Also, run Chrome (or Firefox and I think Safari?) and use your browser's console! You can do so many cool things with the console. Anything you can run in JavaScript, you can run in your browser's console! Chrome gives you access to using document.write()
as well, but Firefox does not.
And one more thing, get acclimated with tracing errors! Syntax errors can be easy to find once you know how to interpret the errors that running JS gives you. You might see errors that don't make any sense to you, but they do make sense once you know why it is giving you that error.
As you go, you'll figure your own method of learning. :)

Marcus Parsons
15,719 PointsHere are some links of some really cool coding sites as well:
The Viking Coding School (is officially recognized by LinkedIn as a university and is FREE until you get a job): http://www.vikingcodeschool.com/
Free Code Camp (an awesome, free coding camp): http://www.freecodecamp.com/
The Odin Project (an open source, free coding camp): http://www.theodinproject.com/
CodeAcademy (has some awesome, interactive tutorials for learning HTML/CSS/JS/jQuery/Ruby/Python/and more all for free): http://www.codecademy.com/

Jason Anders
Treehouse Moderator 145,862 PointsThe only problem I see is you have a semi-colon after the "if" declaration and before its opening curly brace. Remove that and it should be okay.
Jeff Ward
8,978 PointsThanks! I did that and it worked. I have no way of 'testing' this code to see how it runs. Do you have any suggestions?
I just want to make sure it does indeed output the next 20 leap years properly.

Gareth Borcherds
9,372 PointsSo I would actually do the following:
var year = prompt("please input a year to find out the next 20 years");
var counter = 20;
var result=true;
while (counter >= 0) {
if ((year % 4 == 0) && (year % 100 != 0)||(year % 400 == 0)) {
result = true;
console.log(year);
year++;
counter--;
}
else {
year++;
}
}
Here is the reason,
You had your counter wrapped in a function and had no way of calling the function after a value was put in, it wasn't really needed. Also, you are right about your === versus ==. It should be noted that === requires the variables to be the same type. So it will only compare string to a string. So if you have "2004" as a string and you compare it to 2004 as an integer even though == returns true, === would return false because the variables are not the same type. You can use either or, but you have to make sure you are getting rid of variable type issues before you can use === or !== for that matter.
The code I put in does output values. To test it, just get to your console using the developer tools in your browser and you'll be able to run the code inline.
Jeff Ward
8,978 PointsThank you!! That is a huge help!!!
I just have two questions on the changes you made:
- You changed == to !=
year % 100 != 0)
Why did you change it from == to !=, just wondering.
- You also removed the function. Hypothetically, if I wanted to run it with a function would I do this??
var year = prompt("please input a year to find out the next 20 years");
var counter = 20;
var result=true;
function leapYearCalc(year, counter, result) {
while (counter >= 0) {
if ((year % 4 == 0) && (year % 100 != 0)||(year % 400 == 0)) {
result = true;
console.log(year);
year++;
counter--;
}
else {
year++;
}
}
}
leapYearCalc();
Thanks a ton again!!! I just want to ensure that I understand this inside and out.
Jeff Ward
8,978 PointsI re-read your response. Thanks again btw.
So your saying I would not need a function in this case?
Thanks!

Gareth Borcherds
9,372 PointsYou don't need it, but you can call the function as you suggested, except your last line would be leapYearCalc(year, counter, result); not leapYearCalc();
As to your other questions, I just copied your code and changed === to == and !== to !=. When you use 3 operators it's going to compare types of variables, when you use 2 it will try and convert them to the same type.
Jeff Ward
8,978 PointsGREAT THANKS!
Jeff Ward
8,978 PointsJeff Ward
8,978 PointsCan you also please inspect my '===' and '!==' signs. I fear they may be wrong :/.