【发布时间】:2017-04-04 02:34:50
【问题描述】:
我正在尝试编写代码来查找数组中的模式。下面是我的逻辑。我该如何改进它,它是否正确?
var list = [2, 3, 6, 10, 10, 3];
var mode = function(values) {
function compare(a, b) {
return a - b;
};
values.sort(compare);
for (i = 0, j = 1; i < list.length, j < list.length + 1; i++, j++) {
if (list[i] == list[j]) {
console.log(" Its a Mode");
}
else {
console.log("not a mode");
}
}
}
mode(list);
如果i和j的值会到达数组的末尾会是什么情况?
【问题讨论】:
-
这可能是 StackExchange 上 CodeReview 的理想帖子,这里有一个类似的示例 - codereview.stackexchange.com/q/68315/61815。您的核心功能有效,但您可能需要处理错误/异常。
标签: javascript mode