if counter % 2 == 1 I am trying to decode this line – it’s a Rails project and I am trying to figure out what the % does in this if statement.
if counter % 2 == 1 I am trying to decode this line –
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
%is the modulo operator. The result ofcounter % 2is the remainder ofcounter / 2.n % 2is often a good way of determining if a numbernis even or odd. Ifn % 2 == 0, the number is even (because no remainder means that the number is evenly divisible by 2); ifn % 2 == 1, the number is odd.