JavaScript function inside HTML display in browser -
i have problem regarding javascript code. i'm trying output sum in browser, doesn't work. tried every way of displaying message in javascript: console.log()
, document.write()
, document.getelementbyid()
. none of seems work. code looks okay, won't output anything. can me, please?
<!doctype html> <meta charset="utf-8"> <html> <head> <script type="text/javascript"> var sum = 0; for(var = 0; < 1000; i++){ if(i%3 === 0 || i%5 === 0) { sum += i; } console.log('the sum is' + " " +sum); } </script> </head> </html>
this code works fine here. i'm running ie11 , sum displays in console no problem. see snippet below; nothing has been changed.
maybe need open dev console? f12 in ie/edge, or settings > developer tools > console in chrome (i think).
<!doctype html> <meta charset="utf-8"> <html> <head> <script type="text/javascript"> var sum = 0; (var = 0; < 1000; i++) { if (i % 3 === 0 || % 5 === 0) { sum += i; } console.log('the sum is' + " " + sum); } </script> </head> </html>
if want have code output browser window, this, add result dom:
<!doctype html> <meta charset="utf-8"> <html> <head> </head> <body> <div id='resultsdiv'> <h3>results here:</h3> </div> <script type="text/javascript"> var sum = 0; var resultsdiv = document.getelementbyid('resultsdiv'); (var = 0; < 1000; i++) { if (i % 3 === 0 || % 5 === 0) { sum += i; } resultsdiv.innerhtml += 'the sum is' + " " + sum + "<br />"; } </script> </body> </html>
Comments
Post a Comment