alex1997 alex1997 - 3 months ago 11
Javascript Question

Variable value doesn't change after second while loop

I have a variable num which I give it a value. After the first while loop the value changes, but when I try to use the new value on a second while loop, the final result won't depend by the new num..



var a = 1,
b = 13,
c = 0,
p; ///6227020800 /// 48648600 ///1801800 ///1245404160 -> /5

function div(p) {
for (var i = a; i <= b; i++) {
if (p % i !== 0) {
c++;
}
}
if (c === 0) {
return 'YES'
}
return 'NO'
}

var num = 6227020800;

while (div(num) == "YES") {
num /= 2;
}
num = num * 2; // num === 48648600

while (div(num) == "YES") {
num /= 3;
}

num = num * 3; // num should be ///1801800 when num === 48648600

while (div(num) == "YES") {
num /= 5;
}

num = num * 5 // finally num should be 360360 when num === 1801800




Answer

It is unclear to me what you are trying to accomplish, but it seems that after the very first run of div(), c gets sooner or later incremented, and then it will never be 0 again. Therefore, div() will never return "YES" and the following while-loops will never be executed.

Comments