Before writing anything, get clear on what is being asked.
Think of it like this
Line the number up in pairs, like shoes. If everything pairs off with nothing left standing alone, the number is even. The remainder is just "how many are left standing".
One = assigns a value; two == compares two values. Mixing them up is the single most common beginner bug, and in some languages it silently does the wrong thing rather than erroring.
Write a function that takes a whole number and answers one question: is it even?
Give back `true` if it is even, and `false` if it is not. A number is even when dividing it by 2 leaves nothing over.
Example 1
Given n = 4
Answer true
4 divided by 2 is exactly 2, with nothing left over.
Example 2
Given n = 7
Answer false
7 divided by 2 leaves 1 over, so it is odd.
Write the function in the editor, then run it against the examples.
The editor on the right already has the function signature filled in for you. Write your answer inside it and press Run — that checks your code against the examples above only, so you can experiment freely.
Each one nudges you a little further. There is no reveal-the-answer button — working it out is the part that actually teaches you.
Submit against every test, then check you can explain why it works.
Ask for the remainder after dividing by 2, then compare it to 0. The comparison itself produces true or false, so you can return it directly — no if statement needed.
return n % 2 == 0Beginners often write: if the remainder is 0 return true, otherwise return false. That works and is not wrong — but the comparison already IS a true/false value, so returning it directly says the same thing with less noise.
Why does checking n % 2 == 0 work for negative numbers like -6?
Run your code to see the test results.
⌘/Ctrl + Enter to run · ⌘/Ctrl + Shift + Enter to submit
Before writing anything, get clear on what is being asked.
Think of it like this
Line the number up in pairs, like shoes. If everything pairs off with nothing left standing alone, the number is even. The remainder is just "how many are left standing".
One = assigns a value; two == compares two values. Mixing them up is the single most common beginner bug, and in some languages it silently does the wrong thing rather than erroring.
Write a function that takes a whole number and answers one question: is it even?
Give back `true` if it is even, and `false` if it is not. A number is even when dividing it by 2 leaves nothing over.
Example 1
Given n = 4
Answer true
4 divided by 2 is exactly 2, with nothing left over.
Example 2
Given n = 7
Answer false
7 divided by 2 leaves 1 over, so it is odd.
Write the function in the editor, then run it against the examples.
The editor on the right already has the function signature filled in for you. Write your answer inside it and press Run — that checks your code against the examples above only, so you can experiment freely.
Each one nudges you a little further. There is no reveal-the-answer button — working it out is the part that actually teaches you.
Submit against every test, then check you can explain why it works.
Ask for the remainder after dividing by 2, then compare it to 0. The comparison itself produces true or false, so you can return it directly — no if statement needed.
return n % 2 == 0Beginners often write: if the remainder is 0 return true, otherwise return false. That works and is not wrong — but the comparison already IS a true/false value, so returning it directly says the same thing with less noise.
Why does checking n % 2 == 0 work for negative numbers like -6?