Javascript

From Rasmapedia
Revision as of 11:24, 6 September 2024 by Eric Rasmusen (talk | contribs)
Jump to navigation Jump to search

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 -->



    <script>        //Javascript has to go in a special script section.
// Prompt the user for his 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>" ;  
        } //The curly bracket shows this is the end of  the loop;

//Add some  blank lines
outputDiv.innerHTML += " <br><br><br><br><br>" ;  
 }  //The curcly bracket shows this is teh end of the function. 

//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>