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 Basics (Retired) Working With Numbers Doing Math

Andrew Teece
Andrew Teece
4,885 Points

Do you need to create another var?

I tried 2 different methods with this exercise.

  1. Instead of creating another var is wrote the following

document.write('I have been alive for more then ' + secondsPerDay * daysPerWeek * weeksPerYear * yearsAlive);

  1. var imAlive = yearsAlive * weeksPerYear * daysPerWeek * secondsPerDay; document.write('I have been alive more than ' + imAlive + ' seconds!' );

Are both methods acceptable? Or are there "best practices" I should be following in JavaScript?

Thanks!

1 Answer

Steven Parker
Steven Parker
231,236 Points

There are some "best practices", and you will learn about them in more advanced courses. But this particular example is more about how the value might be used later in the program, or how the program might be modified in the future.

If the value for seconds alive is used only for the output line and never again, your approach is as good as the video suggestion. But if that value were needed again later in the program, or even if anticipated future program changes might need it, it would make more sense to create the variable.