Theoretical recap.
Determining whether a number is even or odd relies on the modulo operator, percent, which returns the remainder of a division.
A number is even when dividing it by 2 leaves no remainder, meaning n% 2 equals equals equals 0.
Otherwise, the remainder is 1, or minus 1 for negatives, and the number is odd.
This single operator is the mental model to keep.
If nothing is left over by 2, the number is even.
Remember the concepts you have already practiced.
You declare variables with let and const, you read input from the console, or convert strings to numbers, and you branch decisions with if-slash-else.
Since user input often arrives as a string, you must convert it to a number with number or parseInt before applying modulo.
This is the single most common mistake beginners make.
A second frequent error is confusing equals, assignment, equals-equals, loose comparison, and equals-equals-equals, strict comparison.
Always use equals equals equals when testing the remainder.
Mini example.
Const n equals 7.
If n%2 equals equals equals 0.
Console.log$n is even.
Else.
Console.log$n is odd.
This print 7 is odd.
With this pattern mastered, you are ready to build a small program that reliably classifies any number.
Read the full transcript.
Create an account to read the whole episode, search across every transcript, and follow the shows you care about.
Theoretical recap.
Determining whether a number is even or odd relies on the modulo operator, percent, which returns the remainder of a division.
A number is even when dividing it by 2 leaves no remainder, meaning n% 2 equals equals equals 0.
Otherwise, the remainder is 1, or minus 1 for negatives, and the number is odd.
This single operator is the mental model to keep.
If nothing is left over by 2, the number is even.
Remember the concepts you have already practiced.
You declare variables with let and const, you read input from the console, or convert strings to numbers, and you branch decisions with if-slash-else.
Since user input often arrives as a string, you must convert it to a number with number or parseInt before applying modulo.
This is the single most common mistake beginners make.
A second frequent error is confusing equals, assignment, equals-equals, loose comparison, and equals-equals-equals, strict comparison.
Always use equals equals equals when testing the remainder.
Mini example.
Const n equals 7.
If n%2 equals equals equals 0.
Console.log$n is even.
Else.
Console.log$n is odd.
This print 7 is odd.
With this pattern mastered, you are ready to build a small program that reliably classifies any number.