Difference between revisions of "Javascript"

From Rasmapedia
Jump to navigation Jump to search
Line 24: Line 24:
 
         for (let i = 1; i <= how_many; i++) {
 
         for (let i = 1; i <= how_many; i++) {
 
             outputDiv.innerHTML += "Happy Birthday,  "+  name +"!<br>" ;   
 
             outputDiv.innerHTML += "Happy Birthday,  "+  name +"!<br>" ;   
         }
+
         } //The curly bracket shows this is the end of  the loop;
  
 
//Add some  blank lines
 
//Add some  blank lines
 
outputDiv.innerHTML += " <br><br><br><br><br>" ;   
 
outputDiv.innerHTML += " <br><br><br><br><br>" ;   
  }  //End of function is here
+
  }  //The curcly bracket shows this is teh end of the function.
  
 
//Run the function and print happy birthday 8 times.
 
//Run the function and print happy birthday 8 times.

Revision as of 11:22, 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>" ;  
        } //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>