Javascript

From Rasmapedia
Revision as of 12:21, 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

   

Printing "Happy Birthday, NAME INPUT" 8 Times

//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 +"!
" ; } //Add some blank lines outputDiv.innerHTML += "




" ; } //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>