+ 5
Could someone explain me how this code works line to line? Thanks.
function f(num){ for(var j=2;j<num/2;j++){ if(num%j===0){ return false; } } return true; } document.write(f(37));
5 Réponses
+ 6
Here. Go to the JS tab and read the comments.
https://code.sololearn.com/WGVHiHg52Hen/?ref=app
+ 4
Here I'll just break it down for you:
On line 1, you declare a function f with one variable to take when it is called, num. When the f() function is called, then all of the code within the curly brackets will run, replacing the variable num with whatever has been given when it was called.
On line 2, you start a for loop. This loop first declares a variable j, and sets it to equal to 2. Then, it sets a condition that checks if the value of j is less than the value of num divided by 2, running the code within it if it is. After the code within the brackets run, then j will increase by one and run through the condition again, and will continue doing so until j is not less than num/2.
On the third line, you set an if statement with a condition that checks to see if the value of result of the calculations of num modulo j is 0. A modulo is an operator used to check the value of the remainder of the division two operands. For example, if you were to have the equation 2%5, the result of that would be 1 because 2 can only go into 5 twice before going over, leaving a remainder of 1. For your if statement, if the equation is true, then the code within that if block will run. If it is false, then it will be completely ignored and skipped over.
For lines 4 and 7, you set a statement to return either a value of true or false. It will only return false if the if statement is at any point true, and will break out of the function as soon as it does. If the if statement never returns true, then the for loop would break out and it will return true (line 7).
On the last line, you set a statement to print out the value of the result of 37 going through the function.
+ 4
To be clear, this function returns true if the argument is prime, otherwise it returns false.
It does this by dividing the argument by all numbers between 2 and half the argument to see if any divide the argument evenly. The first one to divide the argument evenly will cause the function to return the value false (the number is clearly not a prime number). If no numbers in the range divide the argument evenly, then you know the argument is a prime number and return the value true.
I know this is not line by line. I thought I would summarize...
+ 3
function f(num){ //basic declaration of function "f" which accepts the value as "num"
for(var j=2;j<num/2;j++){ //variable j starts at the number 2 and increases by 1 every loop. As long as variable j is less than num/2 (which is 18), the loop will continue.
if(num%j===0){ // says "If the value of num (37) % j (2 -> 17) is equal to the value of 0, then return false.
return false;
}
}
return true; //if (num % j) is never equal to 0, then the function returns true.
}
document.write(f(37)); //gives value of 37 to the function f. In this case, the function would return true because (num % j) never equals 0.
+ 3
ok, thanks.