method = function(a) {
//use associative array to keep track of the total number of appearances of a value
//in the array
var counts = [];
for(var i = 0; i <= a.length; i++) {
if(counts[a[i]] === undefined) {
//if the condition is looking for a[0],a[1],a[2],a[3],a[4],a[5] inside counts[] one value at a time and does not find them inside counts[] it will set each value to 1
counts[a[i]] = 1;
console.log(counts[a[i]]);
} else {
return true;
}
}
return false;
}
JS的控制檯日誌1X6
哪有條件看裏面計數的值[A [1] ]如果所有計數[a [i]]值都被設置爲1,每次迭代都會重複?難道它總是比較1比1嗎?例如,如果一個數組是[1,2,3,4,5,2]並且計數[a [1]](數組中的第一個int 2)未定義,然後設置爲1,那麼如何將數組[條件是否知道count [a [5]](數組中的第二個int 2)與counts [a [1]]的值相同,因此它應該返回true?或許我誤解了正在發生的事情。 我將不勝感激任何幫助。感謝
原因計數會[2]將是1,而不是不確定返回true ... –
這是沒有什麼可以解釋的,問題是你沒有得到邏輯... –