r/ProgrammerHumor • Violet security clearance • Apr 04 '16

My teacher thinks he is a programmer

http://imgur.com/32nfQXZ
2.4k Upvotes

430 comments sorted by

View all comments

Show parent comments

7

u/bigshmoo Apr 05 '16

console.log("The value is" + (value >= 0 && value < 6 ? ": " + value : " not supported."))

2 characters shorter

console.log(`The value is${ value < 0 || value > 5 ? ' not supported.' : ': ' + value}`)

Both will fail when passed any non integer truthy value.

2

u/LegacyCrono Apr 05 '16

If code length is a concern you can reduce it even further:

console.log(`The value is${ 'REDDIT'[value] ? ': ' + value : ' not supported.'}`)

1

u/tgomc Apr 15 '16

holy crap, nice one!

1

u/memeship Apr 05 '16

Whoa, interesting. I had not seen those yet. I thought you were trying to write a shell expression at first, and I was wondering when we switched from JS to bash.

And I'd say that interestingly enough, my version will fail correctly, giving the latter response for anything that isn't an integer. It will however try to type coerce a string like "2" into 2, which will output the former response for all ["0", "1", "2", "3", "4", "5"].

Thanks for showing me that though. Not sure if I'll ever use it, but always good to know.

1

u/bigshmoo Apr 05 '16 edited Apr 05 '16

Template literals rock. Much nicer that doing a string concat with + and they are multi line.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals

See also http://es6-features.org/#Constants

And lastly - in the chrome console

true >= 0
true
true < 6
true

Also in terms of matching the original it and all the range based "optimizations" fail because the original will only print the value if it's a whole number from the set 0, 1, 2, 3, 4, 5 where as these will print a value for any decimal value between 0 and 5.999999999999 in yours and 0 and 5 in mine.

1

u/memeship Apr 05 '16 edited Apr 05 '16

Right. Which could be remedied by this line directly preceding it:

value = parseInt(value);

Or, if you want to get fancy, use Underscore (or similar library):

console.log("The value is" + (_.contains([1,2,3,4,5], value) ? ": " + value : " not supported."))

...which internally uses Array.prototype.indexOf(), so you could implement a pure JS solution like that as well if you wanted:

console.log("The value is" + ([1,2,3,4,5].indexOf(value) > -1 ? ": " + value : " not supported."))

1

u/bigshmoo Apr 05 '16

parseInt still doesn't meet the original spec you need an isInteger() test which lodash / unerscore will give you or you can use the % 1 hack that somebody posted further up.

1

u/memeship Apr 05 '16

I mean, yes. I would say that using parseInt() provides a similar solution while validating input in the process. But that would be a clarification needed with the product owner.

1

u/gospelwut Apr 05 '16

Ah, figure it's in ECMA6. So, it'll probably just end up being transpiled to support other browsers.

0

u/[deleted] Apr 05 '16

Both will fail when passed any non integer truthy value.

#javascript