我正在做一個愚蠢的小編碼挑戰,讓我難住。我試圖把鍵值對的列表,像這樣:試圖在javascript中總結鍵值對的列表
約翰:2
簡:3
約翰:4
簡:5
你的目標是計算textarea中每個鍵的計數,並在HTML文檔中顯示每個鍵的總數。默認值應該導致輸出結果爲「總計
約翰是6. Jane的總數是8.」
我試圖做到這一點沒有jQuery或任何其他框架這是我到目前爲止,我不斷獲得NaN的價值。我可能會顛倒我的邏輯?
$(function() {
var keyStore = document.getElementById("keyValPairs");
if (!keyStore){
alert("you suck DIE!!!!!");
}
var hashTable = {};
var str = split(",");
for(var entry in str){
var a = entry.split(":")
if(!hashTable.hasOwnProperty(a[0])){
hashTable[a[0]] = 0;
}
hashTable[a[0]] += parseInt(a[1]);
}
console.log(obj);
});
我使用了jQuery函數來包裝它,因爲我不「T記得說當DOM加載的常規方式
HTML:。
<div class="panel panel-primary">
<div class="panel panel-header">
<h1>Sum up your key value pairs</h1>
</div>
<div class="panel panel-body">
<div class="col-sm-6">
<textarea class="preescreen-input" name="keyValPairs" id="keyValPairs" cols="30" rows="10">
John : 2,
Jane: 3,
John : 4,
Jane : 5,
</textarea>
</div>
<div class="col-sm-2">
<button class="btn btn-primary">Sum</button>
</div>
<div class="col-sm-2">
<button class="btn btn-danger">
Insert
</button></br></br>
<label for="inputBoxKey">Enter Key</label>
<input type="text" id="inputBoxKey" name="inputBoxKey"></br></br>
<label for="inputBoxVal">Enter Value</label>
<input type=" text" id=inputBoxVal name="inputBoxVal">
</div>
<div class="col-2-offset-4 panel panel-footer">
<input type="text ">
</div>
</div>
</div>
它只是一個愚蠢的小編程挑戰,我在網上找到,但沒有更少它讓我困惑。任何幫助將不勝感激。任何幫助將不勝感激。
並不意味着太多 – Guyute