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

JavaScript JavaScript Numbers Working with Numbers Create a Program with Math

Not the quickest in math, but was wondering if this was the correct calculation and code to find num of seconds in years

const secsPerMin = 60;
const minsPerHour = 60;
const hoursPerDay = 24;
const daysPerWeek = 7;
const weeksPerYear = 52;

const secondsPerDay = secsPerMin * minsPerHour * hoursPerDay;
console.log(`There are ${secondsPerDay} seconds in a day.`); 


const yearsAlive = 32;
console.log(`I've been alive for more than ${yearsAlive * weeksPerYear * daysPerWeek * secondsPerDay} seconds!`);

7 Answers

const secsPerMin = 60;
const minsPerHour = 60;
const hoursPerDay = 24;
const daysPerWeek = 7;
const weeksPerYear = 52;


const secsPerDay = secsPerMin * minsPerHour * hoursPerDay;
console.log(`There are ${secsPerDay} seconds in a day`);

const yearsAlive = 45;
const secsAlive = yearsAlive * weeksPerYear * daysPerWeek * secsPerDay;
console.log(`I've been alive for more than ${secsAlive} seconds!`);
benoit koch
benoit koch
3,875 Points

This is what i came up with.

const secsPerMin = 60; const minsPerHour = 60; const hoursPerDay = 24; const daysPerWeek = 7; const weeksPerYear = 52;

const secondsPerDay = secsPerMin * minsPerHour * hoursPerDay; console.log(There are ${secondsPerDay} seconds in a day.);

let yearsAlive; console.log(I've been alive for more than ${+86400 * +7 * +52 * +31} seconds!);

Steven Parker
Steven Parker
231,275 Points

I think the idea was to use the constants and not replicate the literal values. Also, what are the "+" symbols for?

benoit koch
benoit koch
3,875 Points

All good :) Oh, i think it was a mistake on my part, i was thinking for some reason that the system would read it as a string, so i added some precautions but i don't think that would of affected the conclusion. + is the quick form of parseInt. "The parseInt() function parses a string and returns an integer."

Steven Parker
Steven Parker
231,275 Points

No danger of digits being interpreted as a string. Also, be aware that "+" isn't quite an exact replacement for "parseInt". One big difference is how they handle an empty string.

Chris Conwell
Chris Conwell
2,268 Points

Your script will output as a string and how the total if you use template literals (); console.log(I've been alive for more than ${+86400 * +7 * +52 * +31} seconds!);

benoit koch
benoit koch
3,875 Points

Thanks for letting me know, really appreciate the advice! :)

Fabio Dolcemascolo
seal-mask
.a{fill-rule:evenodd;}techdegree
Fabio Dolcemascolo
Front End Web Development Techdegree Student 6,435 Points

const secsPerMin = 60; const minsPerHour = 60; const hoursPerDay = 24; const daysPerWeek = 7; const weeksPerYear = 52; const yearsAlive = 39;

const secondsPerDay = secsPerMin * minsPerHour * hoursPerDay; console.log(There are ${secondsPerDay} seconds in a day.);

const secondsPerWeek = secondsPerDay * daysPerWeek; console.log(There are ${secondsPerWeek} seconds in a week.);

const secondsPerYear = secondsPerWeek * weeksPerYear; console.log(There are ${secondsPerYear} seconds in a year.);

const secondsAlive = secondsPerYear * yearsAlive; console.log(I've been alive for more than ${secondsAlive} seconds!);

const secsPerMin = 60; const minsPerHour = 60; const hoursPerDay = 24; const daysPerWeek = 7; const weeksPerYear = 52;

const secondsPerDay = secsPerMin * minsPerHour*hoursPerDay console.log(There are ${secondsPerDay} seconds in a day.);

const yearsAlive = 36; const secondsPerYear = secondsPerDay * daysPerWeek* weeksPerYear console.log(I've been alive for more than ${secondsPerYear} seconds);