+ 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?
10 Antworten
+ 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
};
+ 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);
+ 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 ;)
+ 2
yh like I said earlier. I replaced it but no effect
+ 2
So, provide the code wich doesn't work, if you want help to fix it :P
+ 2
https://code.sololearn.com/WqsPXUnMbPC0/?ref=app
this code doesn't have any effect on my bracket and Dream weaver
+ 2
please could you help. sorry to reply late
+ 2
thank you but please could you edit the code with what you said and post it here
+ 2
thanks alot
+ 1
thanks visph. just renamed it.