+ 1

Please could someone help make the output of the loop in my code to be only the sum of the last loop. Thanks.

https://code.sololearn.com/WqsPXUnMbPC0/?ref=app

27th Jun 2017, 7:38 PM
Sammy
Sammy - avatar
9 odpowiedzi
+ 8
// Just place console.log statement outside the loops. var sum=0; for ( p = 0; p <=10; p++) { var notPrime = false; for ( i = 2; i <= p; i++) { if (p%i===0 && i!==p) { notPrime = true; } } if (notPrime === false) { sum+=p; } } console.log(sum);
27th Jun 2017, 7:46 PM
Hatsy Rei
Hatsy Rei - avatar
+ 1
thank you hatsy but could you please explain why it happened with the log outside the loop
27th Jun 2017, 8:46 PM
Sammy
Sammy - avatar
+ 1
Everything that is inside the loop happens on each iteration of the loop. If you put it outside to loop it will only happen when the loop is finished.
27th Jun 2017, 9:47 PM
Kevin Oudai
Kevin Oudai - avatar
+ 1
okay thanks Kevin. I appreciate
28th Jun 2017, 5:22 AM
Sammy
Sammy - avatar
+ 1
I tried encapsulating that code in the script tag in html using two different ide (bracket and dream weaver) but it couldn't run. you have any idea why it can't run?
28th Jun 2017, 5:39 AM
Sammy
Sammy - avatar
+ 1
if you are copying these exact codes into a HTML page between the script tags then it is working. I am assuming you are running it on the chrome browser. if you right click on the blank page and then go to inspect. On the tabs you should see tabs (elements, console,..etc.) click on the console and you will see the 18 there. "console.log();" outputs to that console tab.
28th Jun 2017, 2:57 PM
Kevin Oudai
Kevin Oudai - avatar
+ 1
When you are are using document.write(); you have to write it like you would any HTML content. Write it in this form and see if is the way you want it. document.write("<h1> The sum is: " + sum + "</h1>");
28th Jun 2017, 6:28 PM
Kevin Oudai
Kevin Oudai - avatar
0
thanks Kevin but when I changed it to document.write ,I still had a blank page.
28th Jun 2017, 5:25 PM
Sammy
Sammy - avatar
0
k thanks would try it out
29th Jun 2017, 6:10 AM
Sammy
Sammy - avatar