2017-02-26 70 views
0

請原諒我,我對語言和編程實踐並不熟練,而且相當新穎。我創建了一個按鈕,提示用戶在輸入號碼後輸入一個號碼,我創建一個for循環,循環次數與數字相同。例如,用戶輸入4,屏幕將顯示0 1 2 3,然後我有一個按鈕要求用戶輸入一個數字,以查看該數字是否存在於前一個數組中。所以如果用戶輸入3,它會顯示「它存在」,如果他們輸入5,它會顯示「找不到號碼」。我應該創建一個數組來存儲迭代,然後通過搜索數字的函數來運行該數組。尋找指導,感謝您的幫助球員。搜索數組內部的數字

 function getNumber() { 
 
     
 
     var el = document.getElementById("demo"); 
 
    
 
     // Get the user's input and convert it to a number 
 
     var n = parseInt(prompt("Please enter a number"),10); 
 
     
 
     // Set up a string that will become the output. 
 
     var output = " "; 
 
    
 
     
 
     // loop through given number 
 
     for(var i = 0; i < n; i++){ 
 
     
 
     // variable containing iterations 
 
     output += i; 
 
     //var numArray[i] = output; 
 
     } 
 
    
 
     //display iterations 
 
     el.textContent = output; 
 
    
 
    } 
 
    
 
    function findNumber(){ 
 
    \t var sn = parseInt(prompt("Search for number"),10); 
 
    
 
    \t for(var j = 0; j < sn; j++){ 
 
    
 
    \t } 
 
    
 
    } 
 
<button onclick="getNumber()">Click!</button> 
 
<p id="demo"></p> 
 
<button onclick ="findNumber()">Click!</button>

+1

爲了獲得理想的結果,您需要測試的只是輸入的第二個數字是否小於輸入的第一個數字。將數組中的所有數字存儲在範圍內是一種不必要的複雜方式。 – nnnnnn

+0

我覺得它更深入一點。我知道他希望使用二叉搜索功能來查看輸入的數字是否存在於前一個數組中。 –

+2

@LeopoldStoich *他*誰? –

回答

0

有幾個方法可以做到這一點。爲了簡單起見,我將在這裏使用全局變量。

 
 
    // A global variable to store user input 
 
    var userInput;   
 
     
 
    function getNumber() { 
 
     
 
     var el = document.getElementById("demo"); 
 
    
 
     // Get the user's input and convert it to a number 
 
     var n = parseInt(prompt("Please enter a number"),10); 
 
     
 
     // Store the user's input to our global variable 
 
     userInput = n; 
 
     
 
     // Set up a string that will become the output. 
 
     var output = " "; 
 
    
 
     
 
     // loop through given number 
 
     for(var i = 0; i < n; i++){ 
 
     
 
     // variable containing iterations 
 
     output += i; 
 
     //var numArray[i] = output; 
 
     } 
 
    
 
     //display iterations 
 
     el.textContent = output; 
 
    
 
    } 
 
    
 
    function findNumber(){ 
 
     var el = document.getElementById("result"); 
 
    \t var sn = parseInt(prompt("Search for number"),10); 
 
     
 
     // If number is within the range 
 
     if (sn < userInput) { 
 
      el.textContent = "it exists"; 
 
     } 
 
     
 
     // If number is not within the range 
 
     else { 
 
      el.textContent = "number not found"; 
 
     } 
 
    } 
 
<button onclick="getNumber()">Click!</button> 
 
<p id="demo"></p> 
 
<button onclick ="findNumber()">Click!</button> 
 
<p id="result"></p>

1

讓您n可變全球
比比較,如果sn高於n

var n; // Make it global 
 

 
function getNumber() { 
 

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

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

 
    // Set up a string that will become the output. 
 
    var output = " "; 
 

 

 
    // loop through given number 
 
    for (var i = 0; i < n; i++) { 
 

 
    // variable containing iterations 
 
    output += i; 
 
    //var numArray[i] = output; 
 
    } 
 

 
    //display iterations 
 
    el.textContent = output; 
 

 
} 
 

 
function findNumber() { 
 
    var sn = parseInt(prompt("Search for number"), 10); 
 
    var isHigher = sn > n; // n is now accessible in this function 
 
    var message = isHigher ? "Not found" : "Number found!"; 
 
    alert(message); 
 
}
<button onclick="getNumber()">Click!</button> 
 
<p id="demo"></p> 
 
<button onclick="findNumber()">Click!</button>

1

在這個理論的例子,你只需要檢查輸入的第二個數字是否小於第一個數字。如果您想搜索任意數字數組中的數字,可以使用javascript indexOf函數。見下面的例子:

var arr = [1,6,77,888]; 
 

 
if (arr.indexOf(66) > -1) { 
 
alert('number is in array'); 
 
} else { 
 
alert('number is not in array'); 
 
}

+1

此解決方案'arr.indexOf(66)> 0'中的代碼將會丟失索引0處的元素。 –

+0

好的結果。編輯。 – paqash

+0

其實這就是我的教授正在尋找的東西。他只是意識到他沒有指定我們應該返回搜索的數字的索引,如果它存在。 –

1

要搜索的陣列,使用JavaScript陣列的indexOf()方法。原始文章給出了一個例子,用myArray[x]=x填充數組,創建其他解決方案指出的選項。假設你要搜索一個數組的更一般的情況下,可以直接使用indexOf或定義返回truefalse一個功能:在JavaScript

function inArray(myArray, queryValue) { 
    return myArray.indexOf(queryValue) > -1; 
}; 

數組對象與像pop()indexOf()等一些附加的方法。JavaScript對象是關聯數組;此解決方案僅適用於Array對象。數組對象由[]文字或Array()構造函數構造而成。與其他JavaScript關聯數組不同,數組只能具有以int命名的屬性。見Eloquent JavaScript