dev.to staff

Posted on

# Daily Challenge #302 - setAlarm

Write a function named setAlarm which receives two parameters. The first parameter, `employed`, is true whenever you are employed and the second parameter, `vacation` is true whenever you are on vacation.

The function should return true if you are employed and not on vacation (because these are the circumstances under which you need to set an alarm). It should return false otherwise. Examples:

``````setAlarm(true, true) -> false
setAlarm(false, true) -> false
setAlarm(false, false) -> false
setAlarm(true, false) -> true
``````

Good luck!

This challenge comes from Swolebrain on CodeWars. Thank you to CodeWars, who has licensed redistribution of this challenge under the 2-Clause BSD License!

Want to propose a challenge idea for a future post? Email yo+challenge@dev.to with your suggestions!

''

JS

``````const setAlarm = (employed,vacation) =>employed && !vacation
``````

Michael Baas

A solution in Dyalog APL:
APL does not have true/false as a dedicated "logical values", but uses 0 or 1 to indicate false/true. So this can be done with a simple calculation: `employed multiplied by not(vacation)`:

``````{⍺×~⍵}
``````

Here's the table of all possible results:

``````      0 1∘.{⍺×~⍵}0 1
0 0
1 0

``````

OK, I cheated: my fn uses the "dyadic" syntax where you provide a left arg (employed) and a right argument (vacation). I've also written an "enhanced" version that can deal with both syntaxes:

``````      setAlarm←{⍺←⊃⍵ ⋄ ⍺×~⊃⌽⍵}
setAlarm 1 1
0
setAlarm 0 1
0
setAlarm 0 0
0
setAlarm 1 0
1
1 setAlarm 0
1
``````

`

Try it online!

peter279k

Here is the simple solution with PHP:

``````function setAlarm(bool \$employed, bool \$vacation): bool {
if (\$employed === false && \$vacation === true) {
return false;
}
return boolval(\$employed ^ \$vacation);
}
``````

Matt Ellen

Enjoy!

``````function setAlarm()
{
if((typeof arguments[0]) == 'boolean')
{
let [first, second] = arguments;
return first && setAlarm(second===null?second:!second, null);
}
return true;
}
``````

Valts Liepiņš

``````setAlarm :: Bool -> Bool -> Bool