5
\$\begingroup\$

Consider:

c=(a,b)=>(+a||0)+(+b||0)

Yes, it is not good to solve it like that. But I want to shorten it even more. If a is a string and b is a string, then return 0. If one is a number, return it. And if both inputs are numbers, then return the sum (if either string can be cast to a number, treat it as a number).

I tried to shorten this more, but I just can’t.

This is the specification for the task:

Usage

node app.js

Expected behavior

The program returns the sum of two numbers. If no numbers are provided, the result should be 0. If only one number is provided, only that number should be returned.

Tests

node app.js 4 5 # 9

node app.js 4 # 4

node app.js 4 a # 4

node app.js a 4 # 4

node app.js a b # 0

node app.js # 0

Update

It also needs to handle floats as well not only integers.

This is my current code (only the code on line 1 should be optimized to be as small as possible):

c=(a,b)=>(+a||0)+(+b||0)

const input1 = process.argv[2];
const input2 = process.argv[3];

const result = c(input1, input2);

console.log(result);
\$\endgroup\$
7
  • \$\begingroup\$ What if a and b are both strings, but ones which cast into numbers? Your code treats them as numbers, but the description seems to imply we need to actually check if they're strings \$\endgroup\$ Jan 12, 2023 at 14:22
  • \$\begingroup\$ no actually strings that can be casted into a float should be treated as a float \$\endgroup\$ Jan 12, 2023 at 14:32
  • 2
    \$\begingroup\$ Just curious: why do you want to shorten it further? I know which website we're currently on. I just hope not too much golfing code is used in production. \$\endgroup\$ Jan 12, 2023 at 21:57
  • 1
    \$\begingroup\$ This code is not used in production. It is just a bet with my Prof. He said you can't shorten this code any more. \$\endgroup\$ Jan 13, 2023 at 7:25
  • 5
    \$\begingroup\$ I don't have an answer for the actual implementation, but you can win your bet with your Professor by just changing (a,b)=> to a=>b=> for -1 byte. ;) \$\endgroup\$ Jan 13, 2023 at 7:41

2 Answers 2

8
\$\begingroup\$

JavaScript, 15 14 bytes

c=a=>b=>~-a-~b

Try it online!

\$\endgroup\$
1
  • \$\begingroup\$ Crazy, but i actually need a code that also works with floats \$\endgroup\$ Jan 12, 2023 at 14:10
7
\$\begingroup\$

If you only need to handle integers, you can use bitwise operations to convert them into a numbers:

c=(a,b)=>(a|0)+(b|b) // Both ways shown fine

Or even shorter:

c=a=>b=>~-a-~b // (a-1)-(-b-1)

Try it online!

This doesn't work for floats, but this trick seems unmentioned elsewhere, so I've left it here.

\$\endgroup\$
3
  • 1
    \$\begingroup\$ I actually need to handle floats as well. Sorry for the confusion \$\endgroup\$ Jan 12, 2023 at 12:18
  • 1
    \$\begingroup\$ It also doesn't work with integers outside the signed 32-bit range \$\endgroup\$ Jan 12, 2023 at 14:38
  • \$\begingroup\$ Nice to know. I am interested if there will be an even shorter answer which can handle floats \$\endgroup\$ Jan 12, 2023 at 17:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.