2014-09-01 119 views
3

下面我試圖給字符串數組添加一個函數,該函數將一個單詞添加到單詞數組中,並且如果單詞已經在數組中以增加count數組中的相應元素的計數:計算字符串中的唯一字

var words = []; 
var counts = []; 

calculate([a, b]); 
calculate([a, c]); 

function calculate(result) { 
    for (var i = 0; i < result.length; i++) { 
     var check = 0; 
     for (var j = 0; i < tags.length; i++) { 
      if (result[i] == tags[j]) { 
       check = 1; 
       counts[i] = counts[i] + 20; 
      } 
     } 
     if (check == 0) { 
      tags.push(result[i]); 
      counts.push(20); 
     } 
     check = 0; 
    } 
} 

然而輸出原來是這樣的:

詞語= A,b 計數= 2,1

當我希望它是: 詞語= A,b,C count = 2, 1,1

感謝提前任何幫助

+3

你聲明一個'words'數組,但從不使用它。你使用一個永遠不會聲明的'tags'數組。我假設他們的意圖是相同的陣列? – flowstoneknight 2014-09-01 09:10:31

+3

你有第二個循環錯誤..你應該迭代'j'。 'J = 0; j 2014-09-01 09:11:10

回答

1

請檢查這一點: 您可以測試它:http://jsfiddle.net/knqz6ftw/

var words = []; 
var counts = []; 

calculate(['a', 'b']); 
calculate(['a', 'c']); 
calculate(['a', 'b', 'c']); 

function calculate(inputs) { 
    for (var i = 0; i < inputs.length; i++) { 
    var isExist = false; 
    for (var j = 0; j < words.length; j++) { 
     if (inputs[i] == words[j]) { 
      isExist = true 
      counts[i] = counts[i] + 1; 
     } 
    } 
    if (!isExist) { 
     words.push(inputs[i]); 
     counts.push(1); 
    } 
    isExist = false; 
} 
} 

console.log(words); 
console.log(counts); 

輸出是:

["a", "b", "c"] (index):46 
[3, 2, 2] 
1

有幾件事情是錯誤的,這裏的工作代碼:

var words = []; 
var counts = []; 

calculate(["a", "b"]); 
calculate(["a", "c"]); 

function calculate(result) { 
    for (var i = 0; i < result.length; i++) { 
     var check = 0; 
     for (var j = 0; j < words.length; j++) { 
      if (result[i] == words[j]) { 
       check = 1; 
       ++counts[j]; 
      } 
     } 
     if (check == 0) { 
      words.push(result[i]); 
      counts.push(1); 
     } 
     check = 0; 
    } 
} 

Jsbin:http://jsbin.com/hawaco/2/edit?js,console

事情我已經改變了:

  • 更改數組字面量以提供字符串而不是變量名稱:[a,b]["a","b"]
  • 替換爲words
  • tags(大概是一個古老的名稱)實例改變了20年代以1秒
  • 製成的counts[j]更加清晰
  • 固定使用i/j指數
增量

需考慮的事項:

  • 或許使這個字典,而不是一對的數組:{"a":1, "b":2},這會使得對簡單的代碼
  • 通行證在陣列的名稱,以允許其它蓄能器,或該方法和陣列組合成一個單一的對象

簡化:

var seen = {}; 

count(["a", "b"], seen); 
count(["a", "c"], seen); 

function count(words, accumulator) { 
    for (var i = 0; i < words.length; ++i) { 
     if(!accumulator.hasOwnProperty(words[i])) { 
      accumulator[words[i]] = 1; 
     } else { 
      ++accumulator[words[i]]; 
     } 
    } 
} 

結果:

>> seen 
[object Object] { 
    a: 2, 
    b: 1, 
    c: 1 
} 

JSBin:http://jsbin.com/halak/1/edit?js,console

2

將問題分解成具有良好名稱的方法可幫助您計算出邏輯。

嘗試這種情況:

<script type="text/javascript"> 
var words = []; 
var counts = []; 
calculate(["a", "b"]); 
calculate(["a", "c"]); 
console.log(words); 
console.log(counts); 

function calculate(result) { 
    for (var i=0; i<result.length; i++) { 
     if (array_contains(words, result[i])) { 
      counts[result[i]]++; 
     } else { 
      words.push(result[i]); 
      counts[result[i]] = 1; 
     } 
    } 
} 

function array_contains(array, value) { 
    for (var i=0; i<array.length; i++) 
     if (array[i] == value) 
      return true; 
    return false; 
} 

</script> 

輸出:

[ 「一」, 「B」, 「C」]
[]
b 1
Ç 1

1

這裏是我的解決方案(使用對象):

const checkWord = (str) => { 
    let collection = {}; 
    // split the string into an array 
    let words = str.split(' '); 
    words.forEach((word) => { 
    collection[word] = word; 
    }); 
    // loop again to check against the array and assign a count 
    for (let j = 0; j < words.length; j++) { 
    if (words[j] === collection[words[j]]) { 
     collection[words[j]] = 0; 
    } 
    collection[words[j]]++ 
    } 
    console.log(collection); 
}; 

您還可以使用reduce

const checkWord = (str) => { 
    let collection = {}; 
    let words = str.split(' '); 
    words.forEach((word) => { 
    collection[word] = word; 
    }); 
    for (var i = 0; i < words.length; i++) { 
    if (words[i] === collection[words[i]]) { 
     collection[words[i]] = 0; 
    } 
    } 
    let total = words.reduce((occurrences, word) => { 
    collection[word]++ 
    return collection; 
}, 0); 
    console.log(total); 
    };