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) Creating Reusable Code with Functions Random Number Challenge Solution

Show TTH: Practice manual-coding minified JavaScript

Hi, so this isn't a question - just want to 'show' this off. I started to geek out trying to manually code minified JavaScript - because, reasons! I'm finding this fun, challenging and entertaining. Do any of you get a kick out of writing code like this? (obviously not appropriate for a production environment).

I'm thinking about writing a pre-processor that will expand single letters to their full keywords. For example, I'd like to write f r(a,b) that will be expanded by the pre-processor into function r(a,b). Or, perhaps make it more jQuery-esque by writing $r(a,b) that expands to function r(a,b).

function r(a,b){return Math.floor(Math.random()*(((a<b)?b:a)-((a<b)?a:b)+1)+(a<b?a:b))}for(var i=0;i<10;++i){console.log(r(Math.random()*1000,Math.random()*1000))}

1 Answer

So you can remove a couple of sets of parentheses around your first two conditionals in the ternary statements, shave off another 4 characters!

And though it's not recommended, you could leave out the var keyword and space within the for loop, and it would just be a global variable.

Lastly, because you already have a random number function, why not use that again at the end?

function r(a,b){return Math.floor(Math.random()*((a<b?b:a)-(a<b?a:b)+1)+(a<b?a:b))}for(i=0;i<10;++i){console.log(r(r(0,1000),r(0,1000))}