2017-02-27 9 views
0

私は以前同様の質問をしましたが、私は明確ではなかった、私はここで正しい質問をする方法を学んでいる。私は、配列のサイズとなる数値を入力するようユーザーに促しています。そして、ループして、以前に宣言したサイズに達するまで数値で配列を埋めるようにします。私の質問は、適切にユーザーの入力で配列を格納する方法です。プロンプトでアレイの格納

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]; 

    } 
+1

を、ここでは' –

+1

: 'USERINPUT = entries.split( ""); 'あなたはあなたの' userInput'値をオーバーライドします。 –

+1

ユーザが「3」のサイズを入力し、次に「3」の数字を入力することを決定した場合、ユーザは3未満の数字を入力し続ける限り、ループは進むだろう! –

答えて

2

私は少し異なるコードを記述します。あなたが `USERINPUT = entries.split(」「)ここでユーザ入力配列をつかういる

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(" "); 

} 
+0

ああ、どこが間違っているのか分かります、助けてくれてありがとう。 –