Kovacs Akos - 1 year ago 81

Javascript Question

I have a simple, modulo based script, which runs for 3 and 6 digits to

`else`

`function caffeineBuzz(n){`

var returnvalue;

if (n % 3 == 0)

returnvalue = "Java";

if (n % 3 == 0 && n % 4 == 0)

returnvalue = "Coffee";

if (n % 3 == 0 && n % 2 == 0)

returnvalue = "Java" + "Script"

if (n % 4 == 0 && n % 2 == 0)

returnvalue = "Coffee" + "Script"

else

returnvalue = "mocha_missing!"

return returnvalue;

}

`n`

Most specific -> less specific approach helped me, but there are cases, when it returns with wrong value.

`function caffeineBuzz(n){`

var returnvalue;

if (n % 4 == 0)

returnvalue = "Coffee" + "Script"

else if (n % 3 == 0 && n % 4 == 0)

returnvalue = "Coffee";

else if (n % 3 == 0 && n % 2 == 0)

returnvalue = "Java" + "Script"

else if (n % 3 == 0)

returnvalue = "Java";

else

returnvalue = "mocha_missing!"

return returnvalue;

}

Answer Source

This function is the guesstimation of the answer based on comments.

```
function caffeineBuzz(n){
//Storing n's modulos to not calculate them multiple times
var mod3 = (n % 3 == 0)
var mod4 = (n % 4 == 0);
var mod2 = mod4 || (n % 2 == 0);
//Maybe: return (mod3 ? ("Java" + (mod4 ? "Coffee" : "")) + (mod2 ? "Script" : "") : "mocha_missing!");
return (mod3 ? ((mod4 ? "Coffee" : "Java") + (mod2 ? "Script" : "")) : ("mocha_missing!"));
}
```