2017-01-30 15 views
-2

剛開始學習JS,有一個關於循環測試和找到一個答案,這並不成功: 我想怎麼看很多時候數字1出現在arrray中。 當我在數組中出現1時,我所能做的就是得到一個真正的答案。 自上週三以來一直試圖找出這個.. 謝謝!使用「for」循環計數的具體數量有多少次出現在一個陣列

var v = [1, 3, 5, 4, 3, 0, 0, 1, 1]; 

count = 1; 

for (var i = 0; i < v.length; i++){ 

     console.log (count == v[i]) 

} 
+0

http://stackoverflow.com/questions/13389398/finding-out-how-many-times-an-array-elem恩 - 出現 – Booboobeaker

+1

研究一些陣列教程和做一些搜索將有所幫助。基本研究問題在問這裏之前 – charlietfl

+0

'count == v [i]'這個迴歸爲真的原因是,它的意思就像它說'計數與索引值相同的值',所以如果count = 1,v [i] = 1,那麼上面的問題就會有一個真實答案。 – ollie

回答

3

如果你要計算的值出現在陣列中的次數,你首先需要初始化環路以外的變量(如果你在循環初始化,該值將在每次迭代進行復位的那個循環)。 其次,你需要一個條件語句,你會檢查,在這種情況下,如果一個值等於1。作爲一個循環,以及數組中的一個值,我們可以獲得當前索引的值,如v[i](您正確地做了這個)。現在您需要加一個到您的櫃檯,counter++counter = counter + 1;相同。現在我在下面使用的if語句有===這是一個等號運算符,它也檢查兩個值是否屬於同一類型。

var v = [1, 3, 5, 4, 3, 0, 0, 1, 1];  
    var count = 0; 

     for (var i = 0; i < v.length; i++){ 
      if(v[i] === 1){ 
       count++; 
      } 
     } 
      console.log (count); 
+0

謝謝! 你是否知道沒有使用if的其他選項? – Yair

+0

我會看看@Agalo答案 – ollie

+0

@Yair看看我的第二個例子。它刪除了循環和'if'。 –

2

您可以使用filter方法,它返回與指定條件的數組,然後你可以通過使用length財產做一個計數。

var v = [1, 3, 5, 4, 3, 0, 0, 1, 1]; 
 
console.log((v.filter(x => x === 1)).length);

+1

做同樣的工作,但對於開始學習JS的人來說......可能會太過先進,但那是我的看法。 – ollie

+1

像'forEach,map,find,filter等'的學習方法肯定有助於編寫簡潔明瞭的代碼。 – Agalo

+2

計數一個項目,創建一個新的數組是有點超過頂部。 –

0

您可以用各種方法做到這一點,但在你的情況,你需要實際檢查1數組值作爲你循環,你是不是做。

var v = [1, 3, 5, 4, 3, 0, 0, 1, 1]; 
 

 
// Don't assume that there are any occurrences of 1 in the array 
 
count = 0; 
 

 
for (var i = 0; i < v.length; i++){ 
 
    // Test to see if the current array item is 1 and, if so, increment the counter 
 
    
 
    // The "long-hand" way: 
 
    //if(v[i] === 1){ 
 
    // count++; 
 
    //} 
 
    
 
    // Above "if" could also be written using JavaScript's "ternary" operator as this: 
 
    count = (v[i] === 1) ? ++count : count; 
 
} 
 

 
// The report should be after the loop has completed. 
 
console.log ("1 appears in the array " + count + " times.")

這裏還有一個(許多)技術,但是這一個去除循環中,if測試,完全櫃檯。它也需要陣列出來的算法,這反過來,可以使代碼更易於理解的:

var v = [1, 3, 5, 4, 3, 0, 0, 1, 1]; 
 

 
// Turn array into string and (using regular expressions) remove any char that is not 1 
 
var s = v.join("").replace(/[0, 2-9]+/g, ""); 
 

 
// Just check the length of the string after removing non-1 chars: 
 
console.log ("1 appears in the array " + s.length + " times.");

1

關閉!你需要做的是初始化一個計數變量,然後遍歷數組。在每個索引中,檢查元素是否與數字匹配。如果是這樣,你增加計數

var v = [1, 3, 5, 4, 3, 0, 0, 1, 1]; 
 

 
var count = 0; 
 
var number = 1; 
 

 
for (var i = 0; i < v.length; i++){ 
 
    if (v[i] == number) { 
 
     count++; 
 
    } 
 
} 
 

 
console.log(count);

1

你必須增加數量,你只檢查是否計數等於當前項目是什麼

var v = [1, 3, 5, 4, 3, 0, 0, 1, 1]; 
 

 
count = 0; 
 

 
for (var i = 0; i < v.length; i++){ 
 
    var cur = v[i];   // < gets the current item 
 
    if (cur == 1)   // < If the current item is 1 
 
     count += 1;   // < Then increase the count by 1 
 
    console.log (count);  // < Log what the count is 
 

 
}

+0

總是一個不使用可選花括號的好習慣。 –

相關問題