Last active
October 31, 2019 05:54
-
-
Save mumin91/9ab5a520052ea76df952eac7924b7372 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <!DOCTYPE html> | |
| <html lang="en"> | |
| <head> | |
| <meta charset="UTF-8"> | |
| <title>Demo</title> | |
| </head> | |
| <body onload="startTheGame();"> | |
| <h1>Hello, Enter your details</h1> | |
| <div id="firstDiv"> | |
| </div> | |
| <script> | |
| function startTheGame() { | |
| let userIsMarried = confirm("are you married?"); | |
| if (userIsMarried) { | |
| createInputForSpouse(); | |
| } | |
| let userHasChild = confirm("do you have child?"); | |
| if (userHasChild) { | |
| let totalNumberOfChild = getNumberOfChildFromUser(); | |
| createInputsForChild(totalNumberOfChild); | |
| } | |
| } | |
| function createInputForSpouse() { | |
| let inputName = document.createTextNode("Spouse name: "); | |
| document.getElementById("firstDiv").appendChild(inputName); | |
| let inputBox = document.createElement("input"); | |
| inputBox.setAttribute("type", "text"); | |
| inputBox.setAttribute("name", "spouse_name"); | |
| document.getElementById("firstDiv").appendChild(inputBox); | |
| } | |
| function getNumberOfChildFromUser() { | |
| let numberOfChild = prompt("how many?"); //store the user input in the variable | |
| return numberOfChild; | |
| } | |
| function createInputsForChild(numberOfChild) { | |
| for (let i = 0; i < parseInt(numberOfChild); i++) { | |
| let childNumber = i + 1; //the number starts from zero. so, added 1 | |
| let inputName = document.createTextNode("Child " + childNumber + " name: "); | |
| document.getElementById("firstDiv").appendChild(inputName); | |
| let inputBox = document.createElement("input"); | |
| inputBox.setAttribute("type", "text"); | |
| inputBox.setAttribute("name", "child" + childNumber + "_name"); | |
| document.getElementById("firstDiv").appendChild(inputBox); | |
| } | |
| } | |
| </script> | |
| </body> | |
| </html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment