简体   繁体   English

用户选择要显示的提示框数量

[英]User selects the number of prompt boxes to display

Trying to write a javascript code That will enable user to select the number of prompt boxes.尝试编写一个 javascript 的代码,这将使用户能够获得 select 的提示框数量。

Like,The prompt box will say, how many prompts do you want.比如,提示框会说,你要多少提示。 Then if the user inputs 2. Two prompt box will show and if user inputs 3 or 4. That number of prompt boxes will show.然后如果用户输入 2。将显示两个提示框,如果用户输入 3 或 4。将显示该数量的提示框。

    let num = Number(prompt('Enter the number of prompts:'));

if (num=1){
  let name1 = prompt('Enter first user name:');
  let age1 = prompt('Enter first user age');
  
  
  console.log(name1)
  console.log(age1);
}
  
else if (num=2){
  let name1 = prompt('Enter first user name:');
  let age1 = prompt('Enter first user age');
  
  let name2 = prompt('Enter second user name:');
  let age2 = prompt('Enter second user age');
  
  console.log(name1);
  console.log(age1);
  
  console.log(name2)
  console.log(age2);
}
else{
  let name1 = prompt('Enter first user name:');
  let age1 = prompt('Enter first user age');
  
  let name2 = prompt('Enter second user name:');
  let age2 = prompt('Enter second user age');
  
  let name3 = prompt('Enter third user name:');
  let age3 = prompt('Enter third user age');
  
  
  console.log(name1)
  console.log(age1);
  
  console.log(name2)
  console.log(age2);
  
  console.log(name3)
  console.log(age3);
  
}

I would just use a for loop.我只会使用 for 循环。

let num = Number(prompt('Enter the number of prompts:'));

const data = [];

for (let i = 0; i < num; i++) {
  let name = prompt(`Enter user ${i}'s name:`);
  let age = prompt(`Enter user ${i}'s age:`);

  data.push({ name, age });
}

data.forEach(d => {
  console.log(d.name);
  console.log(d.age);
});

Simplify it by rewording the prompts so you can use the index of the current position in the loop.通过改写提示来简化它,以便您可以在循环中使用当前 position 的索引。

If you want everything to be logged at the end, keep all the answers in an array, and iterate through that array at the end to log the answers.如果您希望在最后记录所有内容,请将所有答案保存在一个数组中,并在最后遍历该数组以记录答案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何执行循环,直到用户在提示时选择“取消”? - How to execute loop until user selects Cancel upon prompt? 执行 while 循环 - 我只想在用户在提示中输入 1 或 2 或在提示中选择取消时退出此循环 - Do while Loop - I only want to exit this loop when user has entered 1 or 2 into the prompt or selects the cancel in prompt 如何根据对话框的提示输入用户创建数组 - How to create an array from user input from dialog boxes prompt() 在提示框中输入的值需要插入算法中,以显示用户输入标题的一定数量的变体 - Value entered into prompt box needs to insert into a algorithm to display a certain number of variations of the user entered title 当用户选择单选按钮时如何显示不同的内容? - How to display different content when a user selects a radio button? 提示用户输入但在表单上显示输入 - Prompt User for Input but display Input on Form 如何限制用户在FORM中选择的列表框选项的数量? - How to limit number of List Box options a user selects in a FORM? javascript中提示框出现问题 - Trouble with prompt boxes in javascript 当用户在命令提示符下选择任何字符串时,节点Js不能满足任何请求 - Node Js is not serving for any request when user selects any string in command prompt 提示用户输入然后打印最小的数字 - Prompt user for input then print smallest number
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM