Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

90
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda