← Back to challenges

Alternating between Numbers Two and Eight with a Catch

JavaScriptHardmathnumbers

Instructions

Create a function that takes a number n (integer greater than zero) as an argument, and returns 2 if n is odd and 8 if n is even.

You can only use the following arithmetic operators: addition of numbers +, subtraction of numbers -, multiplication of number *, division of number /, and exponentiation **.

You are not allowed to use any other methods in this challenge (i.e. no if statements, comparison operators, etc).

Examples

f(1) ➞ 2

f(2) ➞ 8

f(3) ➞ 2

Notes

N/A

javascript
Loading editor…
to run
Walks through the solution with reasoning and edge cases.
Next: Hamming Code