Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 提示时使用阵列存储_Javascript_Arrays_Prompt - Fatal编程技术网

Javascript 提示时使用阵列存储

Javascript 提示时使用阵列存储,javascript,arrays,prompt,Javascript,Arrays,Prompt,我之前问过一个类似的问题,但我不清楚,我正在学习如何在这里提出正确的问题。我会提示用户输入一个数字,该数字将是数组的大小,然后循环让他们用数字填充数组,直到达到之前声明的大小。我的问题是如何正确地存储带有用户输入的数组 function getNumber() { var el = document.getElementById("demo"); // Get the user's input and convert it to a number var

我之前问过一个类似的问题,但我不清楚,我正在学习如何在这里提出正确的问题。我会提示用户输入一个数字,该数字将是数组的大小,然后循环让他们用数字填充数组,直到达到之前声明的大小。我的问题是如何正确地存储带有用户输入的数组

function getNumber() {

      var el = document.getElementById("demo");

      // Get the user's input and convert it to a number
      var size = parseInt(prompt("Please enter the size of the array"),10);


        var entries = parseInt(prompt("Enter an integer"),10);

        var userInput = new Array();
        while (entries < size){
             var entries = parseInt(prompt("Enter an integer"),10);
             userInput.push(entries);
             userInput = entries.split(" ");

        }

      // Store the user's input to our global variable
      //userInput[] = entries; 

      // Set up a string that will become the output.

      //display iterations
      el.textContent = userInput[entries];

    }
函数getNumber(){
var el=document.getElementById(“演示”);
//获取用户的输入并将其转换为数字
var size=parseInt(提示(“请输入数组大小”),10);
var entries=parseInt(提示(“输入整数”),10);
var userInput=新数组();
while(条目<大小){
var entries=parseInt(提示(“输入整数”),10);
push(条目);
userInput=entries.split(“”);
}
//将用户的输入存储到全局变量
//userInput[]=条目;
//设置将成为输出的字符串。
//显示迭代
el.textContent=用户输入[条目];
}

我将编写稍微不同的代码:

function getNumber() {

    var el = document.getElementById("demo");

    // Get the user's input and convert it to a number
    var size = parseInt(prompt("Please enter the size of the array"),10);

    // array that will store user input
    var userInput = [];
    while (userInput.length < size){
         var entries = parseInt(prompt("Enter an integer"),10);
         userInput.push(entries)
    }
  //join array element with a space to display
  el.textContent = userInput.join(" ");

}
函数getNumber(){
var el=document.getElementById(“演示”);
//获取用户的输入并将其转换为数字
var size=parseInt(提示(“请输入数组大小”),10);
//将存储用户输入的数组
var userInput=[];
while(userInput.length
我将编写稍微不同的代码:

function getNumber() {

    var el = document.getElementById("demo");

    // Get the user's input and convert it to a number
    var size = parseInt(prompt("Please enter the size of the array"),10);

    // array that will store user input
    var userInput = [];
    while (userInput.length < size){
         var entries = parseInt(prompt("Enter an integer"),10);
         userInput.push(entries)
    }
  //join array element with a space to display
  el.textContent = userInput.join(" ");

}
函数getNumber(){
var el=document.getElementById(“演示”);
//获取用户的输入并将其转换为数字
var size=parseInt(提示(“请输入数组大小”),10);
//将存储用户输入的数组
var userInput=[];
while(userInput.length
您正在这里重击userInput数组
userInput=entries.split(“”)此处:
userInput=entries.split(“”)
您覆盖了
userInput
值。如果用户输入了
3
的大小,然后决定在
3
下面输入数字,那么只要用户继续输入小于3的数字,循环就会继续!我注意到当屏幕只打印出我输入的号码时。解决此问题的好方法是什么?您在这里重击userInput数组
userInput=entries.split(“”)此处:
userInput=entries.split(“”)
您覆盖了
userInput
值。如果用户输入了
3
的大小,然后决定在
3
下面输入数字,那么只要用户继续输入小于3的数字,循环就会继续!我注意到当屏幕只打印出我输入的号码时。解决这个问题的好方法是什么?啊,我知道哪里出了问题,谢谢你的帮助。啊,我知道哪里出了问题,谢谢你的帮助。