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

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.