Share your repls and programming experiences

← Back to all posts
hi
WasaamObaid (0)
<script> 2 ​ 3 document.write("<b>LESSON 1: PRINT HELLO WORLD USING VARIABLES</b><br><br>"); 4 ​ 5 // To start we will create 2 variables: 6 var variable1 = 'Hello '; 7 var variable2 = 'World!<br><br>'; // <br> means line break in html 8 9 // Create a third variable by adding both variables: 10 var final_text = variable1 + variable2; 11 ​ 12 // Print the final result: 13 document.write(final_text); 14 ​ 15 document.write("<b>LESSON 2: SOLVE AN EQUATION</b><br><br>"); 16 ​ 17 var n = 9; 18 var x = n*n; 19 document.write("Result of n*n=" + x); 20 ​ 21 document.write("<br><br><b>LESSON 3: PRINT NUMBERS FROM 1 TO 10</b><br><br>"); 22 ​ 23 // Create a loop of 10 elements. 24 // Variable "i" starts with value 1 and while i<=10 it will increment 1 (i=i+1) 25 for (var i=1; i<=10; i=i+1) { 26 ​ 27 document.write(i); // Print the current "i" number 28 ​ 29 // Print a comma followed by a space if i < 10 30 if (i<10) { 31 document.write(", "); 32 } 33 } 34 ​ 35 function show_random_number() { 36 ​ 37 var random_number = Math.random(); // generate random number between 0 and 1 38 alert(random_number); // show popup with a random number 39 40 } 41 ​ 42 </script>

43

44

<p style="color: green"> 45 The content above was created with JavaScript.<br> 46 This content is created with <b>HTML</b>.<br> 47 You can edit JavaScript and HTML in the left part of the page 48 and click "<b>Run code</b>" to view results in the right part of the page. 49 </p> 50 <b>LESSON 4: CALL A JAVASCRIPT FUNCTION:</b> 51 <input type=button onClick="show_random_number()" value="Generate random number"> 52 </script> 53 ​ ||Resultcreated by Rodrigo Siqueira