2 ms·
I like code-golf challenges. To write shorter code, you have to first find an algorithm that's short to write and also know all the ways you can re-write existi
by XCSme 4y ago
I like code-golf challenges. To write shorter code, you have to first find an algorithm that's short to write and also know all the ways you can re-write existing statements to make them shorter (which are often language-specific). This forces you to learn operator priorities (when you can avoid parentheses), what are default variable values, short-circuited statements, when you can re-use the same variable or ways to use built-in functions for things other than they were built for, etc.
Some code-golf tricks examples, for JavaScript: https://www.geeksforgeeks.org/code-golfing-in-javascript/ https://www.geeksforgeeks.org/code-golfing-in-javascript/
A few examples, if you skip the link:
// Check if variable is upper case
'g' < {} // Returns false, lower case
'G' < {} // Returns true, upper case
If you were to use a 'g' < 'a' you would need an extra character.
// Ceil
a = Math.ceil(a);
a = a%1 ? -~a : a;