Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript JavaScript Basics (Retired) Working With Numbers Doing Math

Andrew Chung
Andrew Chung
5,203 Points

Help 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.");

I 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'

7 Answers

Chandler Tayek
Chandler Tayek
5,513 Points

I'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
Andrew Chung
5,203 Points

Thanks 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
Chandler Tayek
5,513 Points

All 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
Andrew Chung
5,203 Points
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
Andrew Chung
5,203 Points

I 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
Chandler Tayek
5,513 Points

Close! 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
Andrew Chung
5,203 Points

Oh ok, that makes sense now. I appreciate all the help!

Here 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
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Sarah Jee Watson
UX Design Techdegree Graduate 28,185 Points

Where 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');

In 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
Andrew Chung
5,203 Points

Nevermind, I forgot the quotation marks. That was the problem.

You'll get it! Just practice! So far so good!

```h1 { color: blue; }

This is a just a Markdown test, Repeat this is only a Markdown test!
h1 { color: blue; }

This is a just a Markdown test, Repeat this is only a Markdown test!

I finally got it!