DEV Community

Discussion on: Welcome Thread - v15

Collapse
 
polisettymanoj profile image
Manoj Kumar

Hi Sir, Can You answer this question??
Write a vanilla javascript program that returns 0 when input is 1 and 1 when input is 0. Do not use if, for, while, switch, do statements. Write as many implementation as you can think of (minimum 4 ways).

Collapse
 
luisandrestobon profile image
luisandrestobon

Hello. Well, I think this is it:

f1 = (input) => { return (1+input)%2 };

f1(0); // 1
f1(1); // 0

f2 = (input) => { return (1-input)%2 };

f2(0); // 1
f2(1); // 0

f3 = (input) => { return ~input&1 };

f3(0); // 1
f3(1); // 0

f4 = (input) => { return +!input };

f4(0); // 1
f4(1); // 0

I'm waiting for your comments about it. Thank you.

Collapse
 
devwanjihia profile image
Samson Wanjihia

return [1, 0][input]