简体   繁体   中英

Function in JavaScript, don't know how it really works

I need to create an page HTML where there is one question "How many members are in your family?" and there is a button. When I click the button a prompt needs to appear and asks "name of a member of the family". And when you enter it, another prompt needs to appear asking for the age. Finally it will print the age and the name of the person you wrote in a numbered list. The problem is that I have to do it in two different functions, one to print what you wrote in the prompt and the next function needs to work only for the functions. I have the prompts working good but I don't know what to do with the other one. Can someone help?

Here's what I have done:

 "use strict" function newPersonAndAge(personId, newTextElement) { } function addNewPerson() { let item = prompt("Enter the name of a member of your family"); let price = prompt("Enter their age"); }
 <h1>Family</h1> <h3>How many members are on your family?</h3> <ul id="members"> </ul> <button onclick="addNewPerson()">Add Member </button>

Just call the method newPersonAndAge from your addNewPerson method

 "use strict" function newPersonAndAge(name, age){ const list = document.getElementById("members"); const listElement = document.createElement("li"); listElement.innerText = name + " with age " + age; list.appendChild(listElement); } function addNewPerson(){ let name = prompt("Enter the name of a member of your family"); let age = prompt("Enter their age"); newPersonAndAge(name, age); }
 <h3>How many members are on your family?</h3> <ul id="members"> </ul> <button onclick="addNewPerson()">Add Member </button>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM