//a piece from the solution code=>
max = 1;
while (prime <= number) {
if (number % prime == 0) {
max=prime;
number = number / prime;
//a piece from the solution code=>
max = 1;
while (prime <= number) {
if (number % prime == 0) {
max=prime;
number = number / prime;
without the whole code, it’s difficult to say anything about a snippet of it
function largestPrimeFactor(number) {
let prime = 2,
max = 1;
while (prime <= number) {
if (number % prime == 0) {
max = prime;
number = number / prime;
} else prime++; //Only increment the prime number if the number isn’t divisible by it
}
return max;
}
max
is declared as a variable, alongside with prime
in the first statement:
let prime = 2, // line ends with comma!
max = 1;
This is the same as:
let prime = 2; // line ends with semicolon
let max = 1;
As for the second question, max
is repeatedly reassigned in the while loop, so that when the loop ends because the condition isn’t true anymore, max
will have the value of the last (and therefore biggest) factor.
ohhh I get it now, thank you so much! didn’t know that syntax part of comma
It’s not very common to declare variables like that, I for instance never use that syntax but always declare my variables with their own keywords on their own lines - it’s just more readable.