My current focus is on creating a basic program in JavaScript that can identify the largest prime factor of an integer. Here is the code I have developed for this purpose:
let result;
function findFactor(number, half) {
for (let i = 2; i < half; i++) {
if (number % i == 0) {
result = number / i;
findFactor(result, result / 2);
}
}
}
findFactor(30, 15);
console.log(result);
While the efficiency and accuracy of this code remains uncertain, there is a specific issue that has grabbed my attention: After the i = 2
step within the findFactor
function, I noticed that the values of number = 5
, half = 2.5
, and result = 5
. However, without any explicit instruction in the code to make changes, the values suddenly revert back to 15
and 7.5
. This unexpected behavior requires further investigation.