Guest User

Untitled

a guest
Jul 19th, 2018
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.60 KB | None | 0 0
  1. function onlyOne(input1, input2, input3){
  2. output = false;
  3. let truthy = 0;
  4. let falsey = 0;
  5. if (!!input1) {
  6. truthy++;
  7. } else falsey++;
  8. if (!!input2) {
  9. truthy++;
  10. } else falsey++;
  11. if (!!input3) {
  12. truthy++;
  13. } else falsey++;
  14. if (falsey > truthy) {
  15. output = true;
  16. }
  17. if (falsey > 2) {
  18. output = false;
  19. }
  20. return output;
  21. }
  22.  
  23. function onlyOne(input1, input2, input3){
  24. if(input1 && !input2 && !input3) return true;
  25. if(!input1 && input2 && !input3) return true;
  26. if(!input1 && !input2 && input3) return true;
  27. return false;
  28. }
  29.  
  30. function onlyOne(input1, input2, input3){
  31. if(input1 || input2 || input3) {
  32. if(!input2 && !input3) return true;
  33. if(!input1 && !input3) return true;
  34. if(!input1 && !input2) return true;
  35. }
  36. return false;
  37. }
  38.  
  39. const xor = (a, b) => !a ^ !b;
  40. const onlyOne = (a, b, c) => xor(xor(xor(a, b), c), a && b && c)
  41.  
  42. function onlyOne(a, b, c){
  43. if (a) {
  44. // a is true
  45. if (b) {
  46. // a and b are true
  47. return false;
  48. }
  49. // a is true b is false
  50. return c ? false : true;
  51. }
  52. // a is false
  53. if (b) {
  54. // a is false, b is true
  55. return c ? false : true;
  56. }
  57. // a and b are false
  58. return c ? true : false;
  59. }
  60.  
  61. function onlyOne(a, b, c){
  62. if (a) { return b ? false : c ? false : true }
  63. return b ? (c ? false : true) : (c ? true : false);
  64. }
  65.  
  66. const table = [false,true,true,false,true,false,false,false];
  67. function onlyOne(a,b,c){
  68. return table[(a ? 1 : 0) + (b ? 2 : 0) + (c ? 4 : 0)];
  69. }
  70.  
  71. const onlyOne = (a, b, c) => (!!a ^ !!b ^ !!c) ^ (a && b && c);
Add Comment
Please, Sign In to add comment