2014-09-01 167 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); 
    };