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 trialAndrew Chung
5,203 PointsHelp with the years alive code.
Can someone tell me what's wrong with my code? Everything works until I add the last document.write line. Once I add that line everything stops working.
var secondsPerMin = 60; var minsPerHour = 60; var hoursPerDay = 24; var daysPerWeek = 7; var weeksPerYear = 52; var secondsPerDay =secondsPerMin * minsPerHour * hoursPerDay; document.write("There are " + secondsPerDay + " seconds in a day"); var secondsPerYear = secondsPerDay * 365; var yearsAlive = secondsPerYear * prompt("How old are you?"); document.write(I've been alive for " + yearsAlive + " seconds.");
7 Answers
Chandler Tayek
5,513 PointsI'm glad you found your bug. It is a great learning experience to find mistakes like that and fix them yourself. Just a heads up: you can make your code look pretty when you ask a question like this by using the Markdown Cheatsheet. So instead of this:
var secondsPerMin = 60; var minsPerHour = 60; var hoursPerDay = 24; var daysPerWeek = 7; var weeksPerYear = 52; var secondsPerDay =secondsPerMin * minsPerHour * hoursPerDay; document.write("There are " + secondsPerDay + " seconds in a day"); var secondsPerYear = secondsPerDay * 365; var yearsAlive = secondsPerYear * prompt("How old are you?"); document.write(I've been alive for " + yearsAlive + " seconds.");
You get this:
var secondsPerMin = 60;
var minsPerHour = 60;
var hoursPerDay = 24;
var daysPerWeek = 7;
var weeksPerYear = 52;
var secondsPerDay =secondsPerMin * minsPerHour * hoursPerDay;
document.write("There are " + secondsPerDay + " seconds in a day");
var secondsPerYear = secondsPerDay * 365;
var yearsAlive = secondsPerYear * prompt("How old are you?");
document.write(I've been alive for " + yearsAlive + " seconds.");
If you notice, the markdown even highlights the single and double quotes on the last line where your bug was! Super helpful!
Andrew Chung
5,203 PointsThanks for the tip. I've looked into the markdown but it doesn't make any sense to me. Do I put html tags around my code or something to make it work?
Chandler Tayek
5,513 PointsAll you have to do is put three backticks then the name of the language you want to mark up. On a new line you can paste your code in. Then on a new line type three backticks. The backtick is also known as a tilda key which is usually located left of the '1'. Be sure to remember to end with three backticks! It will not properly mark up your code if you don't enclose it with a beginning set of backticks, programming language, then an ending set of backticks. You can hit the eye button in order to preview what the markup will look like before posting. Note the markup will not show up as soon as you type it. You will only see it if you click the preview button in the bottom left or if you post,=.
Try it yourself (if your up for it). Try giving this css code a css markup in a reply comment to my original post:
html, body, div, h1, h2, p, ul, section { margin: 0; padding: 0; border: 0; font: inherit; font-size: 100%; vertical-align: baseline; }
html { line-height: 1; }
ul { list-style: none; }
section { display: block; }
body { background: #edeff0; padding: 50px 0 0; font-family: "Helvetica Neue", Helvetica, Arial, sans-serif; font-size: 62.5%; }
h1, h2 { font-size: 2.4em; font-weight: 400; margin-bottom: 8px; color: #384047; line-height: 1.2; }
h2 { font-size: 1.8em; }
p { color: #8d9aa5; font-size: 1.4em; margin-bottom: 15px; line-height: 1.4; }
.container { box-sizing: border-box; width: 90%; max-width: 1080px; background: white; padding: 30px 15px; margin: 0 auto; position: relative; overflow: hidden; border-radius: 5px; -webkit-box-shadow: 0 1px 0 0 rgba(0, 0, 0, 0.1); -moz-box-shadow: 0 1px 0 0 rgba(0, 0, 0, 0.1); box-shadow: 0 1px 0 0 rgba(0, 0, 0, 0.1); }
.button { background: #3f8abf; padding: 8px 18px; font-family: "Helvetica Neue", Helvetica, Arial, sans-serif; font-size: 1.4em; color: white; font-weight: 500; border: 3px solid #3f8abf; outline: none; cursor: pointer; display: -moz-inline-stack; display: inline-block; vertical-align: middle; *vertical-align: auto; zoom: 1; *display: inline; -webkit-border-radius: 5px; -moz-border-radius: 5px; -ms-border-radius: 5px; -o-border-radius: 5px; border-radius: 5px; -webkit-box-shadow: 0 2px 0 0 #3574a0; -moz-box-shadow: 0 2px 0 0 #3574a0; box-shadow: 0 2px 0 0 #3574a0; -webkit-transition: 0.3s; -moz-transition: 0.3s; -o-transition: 0.3s; transition: 0.3s; }
.button:hover { background: #397cac; border-color: #397cac; -webkit-box-shadow: 0 2px 0 0 #2c6085; -moz-box-shadow: 0 2px 0 0 #2c6085; box-shadow: 0 2px 0 0 #2c6085; }
Andrew Chung
5,203 Pointshtml, body, div, h1, h2, p, ul, section { margin: 0; padding: 0; border: 0; font: inherit; font-size: 100%; vertical-align: baseline; }
html { line-height: 1; }
ul { list-style: none; }
section { display: block; }
body { background: #edeff0; padding: 50px 0 0; font-family: "Helvetica Neue", Helvetica, Arial, sans-serif; font-size: 62.5%; }
h1, h2 { font-size: 2.4em; font-weight: 400; margin-bottom: 8px; color: #384047; line-height: 1.2; }
h2 { font-size: 1.8em; }
p { color: #8d9aa5; font-size: 1.4em; margin-bottom: 15px; line-height: 1.4; }
.container { box-sizing: border-box; width: 90%; max-width: 1080px; background: white; padding: 30px 15px; margin: 0 auto; position: relative; overflow: hidden; border-radius: 5px; -webkit-box-shadow: 0 1px 0 0 rgba(0, 0, 0, 0.1); -moz-box-shadow: 0 1px 0 0 rgba(0, 0, 0, 0.1); box-shadow: 0 1px 0 0 rgba(0, 0, 0, 0.1); }
.button { background: #3f8abf; padding: 8px 18px; font-family: "Helvetica Neue", Helvetica, Arial, sans-serif; font-size: 1.4em; color: white; font-weight: 500; border: 3px solid #3f8abf; outline: none; cursor: pointer; display: -moz-inline-stack; display: inline-block; vertical-align: middle; *vertical-align: auto; zoom: 1; *display: inline; -webkit-border-radius: 5px; -moz-border-radius: 5px; -ms-border-radius: 5px; -o-border-radius: 5px; border-radius: 5px; -webkit-box-shadow: 0 2px 0 0 #3574a0; -moz-box-shadow: 0 2px 0 0 #3574a0; box-shadow: 0 2px 0 0 #3574a0; -webkit-transition: 0.3s; -moz-transition: 0.3s; -o-transition: 0.3s; transition: 0.3s; }
.button:hover { background: #397cac; border-color: #397cac; -webkit-box-shadow: 0 2px 0 0 #2c6085; -moz-box-shadow: 0 2px 0 0 #2c6085; box-shadow: 0 2px 0 0 #2c6085; }
Andrew Chung
5,203 PointsI think I might've made a mistake. I put 3 backticks and then css and then 3 more backticks. Then I put the code on the next line down.
Chandler Tayek
5,513 PointsClose! Do not put three ticks after the language until after the code and on a new line. So here is the outline:
[three ticks][programming language]
code here
[three ticks]
You had:
[three ticks][programming language][three ticks]
code here
Andrew Chung
5,203 PointsOh ok, that makes sense now. I appreciate all the help!
Chandler Tayek
5,513 PointsYeah no problem :)!
Jeff Ward
8,978 PointsHere is my code for calculating how many seconds someone has been alive for:
var secondsPerMin = 60;
var minsPerHour = 60;
var hoursPerDay = 24;
var daysPerWeek = 7;
var weeksPerYear = 52;
var secondsPerDay = secondsPerMin * minsPerHour * hoursPerDay;
document.write('There are ' + secondsPerDay + 'seconds in a day.');
var yearsAlive = prompt("How old are you?");
var YearsAliveCalc = yearsAlive * secondsPerDay * daysPerWeek * weeksPerYear;
document.write("<h2>You have been alive for " + YearsAliveCalc + " Seconds </h2>");
Sarah Jee Watson
UX Design Techdegree Graduate 28,197 PointsWhere am I going wrong?!
var secondsPerMin = 60;
var minsPerHour = 60;
var hoursPerDay = 24;
var daysPerWeek = 7;
var weeksPeryear = 52;
var secondsPerDay = secondsPerMin * minsPerHour * hoursPerDay;
document.write('There are ' + secondsPerDay + ' seconds in a day');
var YearsAlive=prompt("How old are you"?);
var secondsPerYear = secondsPerDay * 365;
document.write('I\'ve been alive for ' + secondsPerYear * yearsAlive + ' seconds');
Jeff Ward
8,978 PointsIn the future please post what the problem is. Such as, I am getting an error and its not running. I can't get this function to work.
From what I can see your error in your code is here:
var YearsAlive=prompt("How old are you"?);
It should be:
var YearsAlive=prompt("How old are you?");
You accidentally had the '?' outside the quotes. I make mistakes like that all the time lol. What I suggest is running the code in Safari or Chrome 'Javascript' console under the developer issues. It tells you what the error is and usually points you where that error is located in your code.
Happy coding!! :D
Andrew Chung
5,203 PointsNevermind, I forgot the quotation marks. That was the problem.
Sarah Jee Watson
UX Design Techdegree Graduate 28,197 PointsSo near and yet so far! Thank you.
Jeff Ward
8,978 PointsYou'll get it! Just practice! So far so good!
Jeremy Franklin
8,512 Points```h1 { color: blue; }
This is a just a Markdown test, Repeat this is only a Markdown test!
Jeremy Franklin
8,512 Pointsh1 { color: blue; }
This is a just a Markdown test, Repeat this is only a Markdown test!
Jeremy Franklin
8,512 PointsI finally got it!
Arlene TIlly
1,619 PointsArlene TIlly
1,619 PointsI have followed for example: var secondsPerMin = 60; var minsPerHour = 60; var hoursPerDay = 24; var daysPerWeek = 7; var weeksPerYear = 52; var secondsPerDay =secondsPerMin * minsPerHour * hoursPerDay; document.write("There are " + secondsPerDay + " seconds in a day"); var secondsPerYear = secondsPerDay * 365; var yearsAlive = secondsPerYear * prompt("How old are you?"); document.write(I've been alive for " + yearsAlive + " seconds.");
When I preview all I get is 'Time calucator'