Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

88
Views
Confused by result of modulo in Solidity

Just going through some learning pains and wanted to know if anyone could help.

My current goal on the course I'm on is to return a bool value from a function depending on if the 2nd uint divides evenly into the first uint.

I have the correct false logic in place according to the site but it still fails on the true logic.

This is the current function I have created

function dividesEvenly(uint x, uint y) public pure returns(bool) {
    if (y % x == 0) {
        return true;
    } else if (y % x != 0) {
        return false;
    }
 }

One of the test cases is (4,2) which I input into it myself mainly and it does chuck out a false. However doing 4 % 2 provides 0 which should return true so I'm not entirely sure what I've done wrong here.

Any help appreciated.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

The modulo operation in your snippet is reversed - y % x. So instead of 4 % 2 (result 0), it calculates 2 % 4 (result 2).

Solution: Reverse back the arithmetics

// the reminder after dividing 4 by 2 is 0
// 4 % 2 == 0

if (x % y == 0) {
    return true;
}
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!