I wrote a function to calculate the modulus of a function but when I run it in the console it always returns undefined. If I use the console.log I can see that it is calculating the correct value.
function modulo(sum, divider){ 
  function loop(difference){ 
    if(difference < divider){ 
     console.log(difference)
     return difference
    } else { 
       setTimeout(loop(difference - divider), 0)
    } 
  } 
  
  return loop(sum - divider) 
}
modulo(8, 5) // 3what I want is this to return the answer e.g.
var result = modulo(8, 5) // 3
Update:
A better solution to this problem would be
modulo = function (x,y){ return x - y * Math.floor(x/y) }
 
     
     
    