+ 3

I tried running this code using document.write on Bracket and Dream weaver but I didn't see any change. Pls what's the problem?

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

5th Jul 2017, 6:24 PM
Sammy
Sammy - avatar
10 ответов
+ 4
Or just use alert() for displaying result in scrippets: this is commonly unadvised because not user friendly, but can do the workaround in such cases ;) 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; } } alert(sum); Anyway, your code doesn't do strictly the << sum of first ten prime numbers >> but rather the sum of prime numbers lesser than ten :P (or equal, but 10 isn't a prime)... Way of doing safely with 'document.body.innerHTML': window.onload = function() { 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; } } document.body.innerHTML = sum; // this will replace all existing content of <body> element };
7th Jul 2017, 5:36 AM
visph
visph - avatar
+ 4
You doesn't use 'document.write()' in this code, but 'console.log()', so the output occurs only in the console, not in the main display ^^ // Created by Sammy 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); // try by replacing last line by: document.write(sum);
6th Jul 2017, 12:48 AM
visph
visph - avatar
+ 3
Check that link: https://helpx.adobe.com/dreamweaver/using/writing-editing-code.html#main-pars_heading_33 It seems that Dreamweaver and some script import/export feature has some limitation with document.write()... Maybe Bracket have also similar limitation ^^ Else, workaround (and better practice) would be to use document.body.innerHTML (but it suppose that DOM element is already loaded when running script (put it in the <body> of your html, or use the window.onload event to detect time when DOM is ready to be fully accesssed ;)
7th Jul 2017, 5:06 AM
visph
visph - avatar
+ 2
yh like I said earlier. I replaced it but no effect
6th Jul 2017, 3:10 AM
Sammy
Sammy - avatar
+ 2
So, provide the code wich doesn't work, if you want help to fix it :P
6th Jul 2017, 3:15 AM
visph
visph - avatar
+ 2
https://code.sololearn.com/WqsPXUnMbPC0/?ref=app this code doesn't have any effect on my bracket and Dream weaver
7th Jul 2017, 4:45 AM
Sammy
Sammy - avatar
+ 2
please could you help. sorry to reply late
7th Jul 2017, 4:45 AM
Sammy
Sammy - avatar
+ 2
thank you but please could you edit the code with what you said and post it here
7th Jul 2017, 5:27 AM
Sammy
Sammy - avatar
+ 2
thanks alot
7th Jul 2017, 5:52 AM
Sammy
Sammy - avatar
+ 1
thanks visph. just renamed it.
7th Jul 2017, 7:45 AM
Sammy
Sammy - avatar