2016-10-11 24 views
1

我試圖通過使用輸入在數組中搜索來查找數字。 有任何想法,爲什麼這不起作用?試圖通過輸入來查找數組中是否存在數字

每次我運行代碼,我只得到的消息:

「號碼不存在」

var arr = [18, 21, 34, 42, 65, 63, 39, 13, 15, 24, -1, 14, 15]; 

var number = document.getElementById("find").value; 

var svar = ""; 

function exists(){ 

    for(i=0; i < arr.length; i++){ 
     if(parseInt(arr[i]) == parseInt(number)){ 
       svar++; 
       document.getElementById("existsArray").innerHTML = tall + "Number exists"; 

     } else { 
       document.getElementById("existsArray").innerHTML = tall + "Number does not exist";  
     } 
    } 
} 

<p id="existsArray"></p> 
<input placeholder="what number would you like to find?" id="find" type="number"> 
<button type="button" onclick="exists()">Finn tallet</button> 
+3

找到匹配元素後,您需要「中斷」。你仍然在運行數組,所以它不會工作,除非數組中的最後一個元素是匹配的元素。 –

+0

什麼是高大的,爲什麼你有svar –

+1

另外,一些註釋:由於你的數組已經包含數字,所以你不需要'parseInt'它們。另外,由於'number'在循環中不會改變,所以你只需要'parseInt'一次,然後在循環中使用該值。最後,您可以使用[Array.prototype.find](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/find)或[Array.prototype.indexOf](https ://developer.mozilla。org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/indexOf)使這更簡單。 –

回答

1

我換成你for循環與indexOf

如果您仍然想使用循環,你應該打破當你找到匹配號碼

var arr = [18, 21, 34, 42, 65, 63, 39, 13, 15, 24, -1, 14, 15]; 
 
var svar = 0; 
 

 
function exists() { 
 
    var number = +document.getElementById("find").value; 
 
    if (arr.indexOf(number) !== -1) { 
 
    svar++; 
 
    document.getElementById("existsArray").innerHTML = "Number exists"; 
 
    } else { 
 
    document.getElementById("existsArray").innerHTML = "Number does not exist"; 
 
    } 
 
}
<input type="number" id="find" /> 
 
<button onclick="exists();">Does it exist ?</button> 
 
<p id="existsArray"></p>

如果你想要得到你應該利用這種事件的數量:

var occurrences = arr.filter(function (num) {return num === number;}).length 
+0

謝謝!對於'for'循環來說,這很容易。 – Theskils

+0

我必須問。如果我想知道數組中存在多少次我輸入的數字。我需要添加到現有的代碼中? – Theskils

+0

@LarsAntonsen編輯 – Weedoze

0

所以你的問題是,當你找到匹配你不退出循環數。因此,除非您要查找的數字是數組中最後一個數字,否則它將保持循環,並且將執行else子句。

function exist() { 
    var number = parseInt(document.getElementById("find").value,10); 
    for(i=0; i < arr.length; i++){ 
     if (exists === arr[i]) { 
       // number exists 
       break; // <-- this is important another alternative would be to just 
         // return at this point if the function doesn't do anything else 
     } 
     else { 
       // this number doesn't match, so we'll keep searching 
     } 
    } 
} 

當然,這是容易得多,如果你只是使用內置的功能Array.prototype.findArray.prototype.indexOf

0

你也可以使用一個過濾器,只保留值的數組至極匹配輸入:

var arr = [18, 21, 34, 42, 65, 63, 39, 13, 15, 24, -1, 14, 15]; 
var input = "65"; 
var result = arr.filter(item => item === parseInt(input)); 
if (result.length === 0) console.log("number doesn't exist"); 
else console.log("number exists"); 
0

我已經對你的代碼進行了一些修改,以幫助隔離你的測試用例。如果你看看現有代碼的這種返工,你會看到你得到了每個數組元素的消息,以「數字不存在」結尾,這是你原來的問題。這是因爲這是最後一條信息,覆蓋了你以前的積極結果。

var number = "42"; 
 
//var svar = ""; 
 
var svar = 0;//changing this from a string to a number. Can't ++ a string. 
 
var myArray = [18, 21, 34, 42, 65, 63, 39, 13, 15, 24, -1, 14, 15]; 
 
/* 
 
* @param {String} num - Passing the value that I'm looking for, rather than 
 
* trying to pull it from elsewhere. This makes this much easier to test later. 
 
* @param {Array} arr - Array of integers to search 
 
*/ 
 
function exists(num, arr) { 
 
    for(i=0; i < arr.length; i++){ 
 
    //if(parseInt(arr[i]) == parseInt(number)){ 
 
    //No need to use parseInt here on the array. It's already a Number. 
 
    if(arr[i] == parseInt(number)){ 
 
     svar++;/* I don't see any reason to be incrementing this. Perhaps it's elsewhere 
 
      in your implementation? */ 
 
     //Using console.log() instead of elements not included in your code sample 
 
     console.log("Number exists"); 
 
    } else { 
 
     //This keeps overwriting the above, except in a case where 
 
     //the last number would be a match! 
 
     console.error("Number does not exist"); 
 
    } 
 
    } 
 
} 
 

 
exists(number, myArray);

如果你想按預期這個工作,你可以可以消除你的「號碼不存在」 else分支,這將導致積極的信息留下來,你可以離開默認的文本爲「號碼不存在」,或者你把它簡化,使用的是什麼我建議:

var number = "42", 
 
    number2 = "101", 
 
    myArray = [18, 21, 34, 42, 65, 63, 39, 13, 15, 24, -1, 14, 15]; 
 

 
var existsSimple = function (num, arr) { 
 
    return myArray.filter(function (itemNum) {return itemNum === parseInt(num);}).length > 0; 
 
}; 
 

 
console.log('Number exists: ' + existsSimple(number, myArray));//true 
 
console.log('Number exists: ' + existsSimple(number2, myArray));//false

相關問題