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 trialDamien Lavizzo
4,265 PointsMy Solution (no console logging)
I'm lazy and I hate hitting F12 every time to check the console, so I used what I learned from another lesson and logged everything right to the Main element.
(The function toLocaleString converts the number to a string and adds proper seperators. I found this in Mozilla's developer reference guide for Javascript.)
const secsPerMin = 60;
const minsPerHour = 60;
const hoursPerDay = 24;
const daysPerWeek = 7;
const weeksPerYear = 52;
const secondsPerDay = secsPerMin * minsPerHour * hoursPerDay;
const secondsPerYear = secondsPerDay * 365
let currentAge = prompt("Please input your age:")
let ageInSeconds = currentAge * secondsPerYear
let ageInWeeks = (currentAge * weeksPerYear).toLocaleString()
document.querySelector('main').innerHTML = `<h2>There are: ${secondsPerDay.toLocaleString()} seconds per day.</h2>
<p>You have been alive for approximately <strong>${ageInSeconds.toLocaleString()}</strong> seconds.
<p>In weeks this would be approximately <strong>${ageInWeeks} weeks.`;
1 Answer
Steven Parker
231,269 PointsBe aware that this only works in a browser. The original code (using console.log) would also run in a host-based JavaScript engine such as node.js, but this would not.
But this is a very useful technique in front-end development, and as you say will be covered in other lessons.
Damien Lavizzo
4,265 PointsDamien Lavizzo
4,265 PointsI didn't know that, thanks for that! Seems like something that's good to know depending on your use case. Very much appreciated.