【发布时间】:2018-07-21 12:40:19
【问题描述】:
我正在准备在 Hacker Rank 上进行 preparation questions 的面试,我想在这方面做得更好。是否有可能得到一些反馈?如何改进我的代码?你是怎么解决this的问题的?
function getCount(array){
let counts = {}
for(let word of array){
let count = counts[word]
counts[word] = count ? counts[word] + 1: 1;
}
return counts
}
// Complete the checkMagazine function below.
function compareNoteMag(note,mag){
let noteKeys = Object.keys(note)
let string = 'Yes'
for(let key of noteKeys){
if(!mag[key]) string = 'No'
if(mag[key] < note[key]){
string = 'No'
}
}
console.log(string)
}
function checkMagazine(magazine, note) {
let magazineCount = getCount(magazine);
let noteCount = getCount(note);
compareNoteMag(noteCount,magazineCount)
};
【问题讨论】:
-
我不确定,但如果您在 codereview.stackexchange.com 询问这个问题可能会更好
-
谢谢@Max 我在那里得到了一些很好的答案:D
标签: javascript arrays hashmap