I am trying to run a function to cancel common factors between two numbers. The cancelling function is outside of my wrapper function “multiplyFraction” and it won’t change the variables inside of multiplyFraction.
function cancelFac(num1,num2){
var minVal = Math.min(num1,num2);
for(var i=2; i<=minVal; i++){
while(num1%i==0 && num2%i==0){
num1 = num1/i;
num2 = num2/i;
}
}
}
var multiplyFraction = function(){
var a = notZero(rand(13,0));
var b = notZero(rand(13,0));
//Cancel Common factors
cancelFac(a,b);
}
The values of a and b remain unchanged. When I put the for loop directly inside of multiplyFraction it works great, but I wanted to make cancelling factors it’s own function because I need to do it so often. Any ideas?
The values of a and b will remain unchanged because they are primitive types and primitive types are passed by value than reference. If you want to maintain the changes done to vlaues being passed then pass the values by reference i.e pass the object instead of primitive types. Or return an object from
cancelFacmethod. Try this.