Cut And Paste Code For Lesson 2


Hello World Code:

 document.write("Hello World")

or

window.alert("Hello World");


Variables, simple addition:

var value1;
var value2;
var answer;
value1 = 2;
value2 = 5;
answer = value1 + value2;
document.write(answer);

 


Strings:
var myvalue;
myvalue = "Hello";
myvalue2 = " World";
document.write(myvalue + myvalue2);


Function:

function hello () {
document.write("hello");
}

hello ();


Loops:

var i;
for(i=10; i>=0; i= i-1){
      document.write(i+"<br>");
}

or

var i;
for(i=10; i>=0; i= i-1){
   var s;
   for(s=0; s<i; s = s+1){
    document.write("*");
   }
   <!-- I want this to print a new line --!>
document.write("<br>");
 
}


Input:

 

<!DOCTYPE html>
<html>
<body>
 
<button onclick="myFunction()">Click to enter your name</button>
<p id="demo"></p>
 
<script>
function myFunction() {
    var person = prompt("Please enter your name", "Darth Vader");
    
    if (person != null) {
        document.getElementById("demo").innerHTML =
        "Hello " + person + "! How are you today?";
    }
}
</script>
 
</body>
</html>

 

 

Or, you can also create a "form" on a web page with multiple blanks to fill in.  Here is a simple example of a web page form:

 <html>
<head>
<form name="EnterName">
     Type a name you've always liked:
     <br><br>
    <input type=text name="myName" size=40>
    <br>
      <input type=button value="Enter" onClick=getName()>
      <br><br><br>
            <input type=text name="Greeting" size=40>
<p>
</form>
<script>
function getName() {
   document.EnterName.Greeting.value=
      "Poof!  You are now: " + document.EnterName.myName.value + "!"
}
</script>
</body>
</html>