For example, if I have a function that is guaranteed to receive either 5 or 7 as an argument, I want the function to return 5 if received 7 and 7 if received 5 without using any conditions.
I was asked this in an interview and was pretty stumped, thanks.
Simple arithmetic:
return 7 - input + 5;
(which can be simplified as return 12 - input;
)
Let's say the input is 7:
return 7 - 7 + 5 --> return 5
Or if the input is 5:
return 7 - 5 + 5 --> return 7
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With