loading...

Daily Challenge #302 - setAlarm

thepracticaldev profile image dev.to staff ・1 min read

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
Enter fullscreen mode Exit fullscreen mode

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!

Discussion

pic
Editor guide
Collapse
_bkeren profile image
''

JS

const setAlarm = (employed,vacation) =>employed && !vacation
Enter fullscreen mode Exit fullscreen mode
Collapse
mbaas2 profile image
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):

{⍺×~⍵}
Enter fullscreen mode Exit fullscreen mode

Here's the table of all possible results:

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

Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode


`

Try it online!

Collapse
peter279k profile image
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);
}
Enter fullscreen mode Exit fullscreen mode
Collapse
mellen profile image
Matt Ellen

Enjoy!

function setAlarm()
{
  if((typeof arguments[0]) == 'boolean')
  {
    let [first, second] = arguments;
    return first && setAlarm(second===null?second:!second, null);
  }
  return true;
}
Enter fullscreen mode Exit fullscreen mode
Collapse
cipharius profile image
Valts Liepiņš

Haskell

setAlarm :: Bool -> Bool -> Bool
setAlarm employed vacation = employed && not vacation
Enter fullscreen mode Exit fullscreen mode