Difference between revisions of "Javascript"

From Rasmapedia
Jump to navigation Jump to search
Line 40: Line 40:
 
   
 
   
 
     </script>
 
     </script>
<pre>
+
</pre>

Revision as of 12:21, 6 September 2024

An Example of a Web Page that asks the user for input and then produces some output

   <h1>Printing "Happy Birthday, NAME INPUT" 8 Times</h1>
    
    <div id="output"></div> <!-- This is where we will display the output we create below -->


//Javascript has to go in a special script section.
    <script>
// Prompt the user for their name
            let name = prompt("What is your name?")  ;

//We will this variable later.
 let how_many=8;

function new_line(){

        // Select the output div
        let outputDiv = document.getElementById('output');

 
        // Print Happy Birthday  8 times
        for (let i = 1; i <= how_many; i++) {
            outputDiv.innerHTML += "Happy Birthday,  "+   name +"!<br>" ;  
        }

//Add some  blank lines
outputDiv.innerHTML += " <br><br><br><br><br>" ;  
 }  //End of function is here

//Run the function and print happy birthday 8 times.
new_line();

// Prompt the user for a number of additional times to repeat happy birthday." how_many" is already declared, so no "let" is needed or allowed;
  how_many = prompt("How many times would you like to see that?")  ;

//Run the function again, after a .4 second delay.
let timer = setTimeout(new_line, 400);
 
    </script>