出于某种原因,我无法理解此代码中的内容
function mode(arr){
var modecount = {}; // creating an object to add number and its mode
for(var i = 0; i < arr.length; i++){ //iterating through array
if(!modecount[arr[i]]){ /*This part I don't understand what do these two lines mean?
I interpret it as if the current number in the array doesn't equal a number already
in the modecount object then it equals 0 */
modecount[arr[i]] = 0;
}
modecount[arr[i]] += 1; /* also what is happening here it is adding 1 to the value
of modecount but why does it not add 1 to unique numbers and how does the code know
to add 1 if it finds duplicates? */
}
return modecount;
}
mode([3,4,3,43,4,34,34,3,3,3,3,3]);
似乎我已经盯着这段代码好几个小时而且无法得到它。请有人向我解释,就像我3岁那样。
答案 0 :(得分:1)
它基本上计算传递的数组中特定值的存在时间,但基值为0时,首先出现该值以初始化该值的属性。
尽管modecount
被指定为对象,但是以可变方式访问对象属性的方式是使用括号表示法。
mode([3,4,3,43,4,34,34,3,3,3,3,3]);
将是:
{ 3: 7, 4: 2, 43: 1, 34: 2 }
答案 1 :(得分:1)
我假设第一部分只是简写,以检查modecount[arr[i]]
是否已初始化/定义。如果它返回“falsey”值,则表示未初始化(访问modecount[arr[i]]
返回undefined
)或者值为0 - 后者不可能,因为下一行总是立即将0增加到1.
请注意,您可以将此检查替换为if(typeof(modecount[arr[i]]) === "undefined")
,以获得等效和更明确的行为。
关于函数的整体操作,请考虑以下pesudocode:
var modecount = {}
foreach element in array:
if modecount[element.value] is not defined:
modecount[element.value] = 0
modecount[element.value]++
答案 2 :(得分:1)
我希望这会有所帮助。我重写了几个变量,因为我感觉它读得更好。
function mode(arr){
// create an index for our counter
var index = {};
// a normal for-loop to iterate through the passed in numbers
for(var i=0, num; i<arr.length; i++){
// each time the foor loop runs, set num to arr[i]
num = arr[i];
// if no valid counter was found...
if(!index[num]){
// then create a new counter; initialize to 0
index[num] = 0;
}
// add 1 to the counter
index[num] += 1;
}
// return the counters
return index;
}
mode([3,4,3,43,4,34,34,3,3,3,3,3]);
答案 3 :(得分:1)
以下是高级别的解释:
这是一个数字列表,它计算每个数字出现在该列表中的次数。
现在,这是内联解释:
function mode(arr){
// Initialize an object literal
var modecount = {};
for(var i = 0; i < arr.length; i++){
// If the value of arr[i] does NOT exist in the object modecount
if (!modecount[arr[i]]) {
// Set the count of arr[i] to zero
// Why? Because it if doesn't exist
// then doing modecount[arr[i]] += 1 will throw an error
// because modecount[arr[i]] will be undefined.
modecount[arr[i]] = 0;
}
// We increase the count of arr[i] in modecount by 1.
// You asked about duplicates.
// It handles duplicates fine because modecount
// acts as a hash map (every key is unique).
// Doing modecount[5] over and over will
// give you the same value associated with 5.
modecount[arr[i]] += 1;
}
return modecount;
}
// Call the function with our array
mode([3,4,3,43,4,34,34,3,3,3,3,3]);
我认为这个逻辑虽然正确,但可以改进。在第一个IF语句之后赋值为0会降低清晰度。如果它不存在,为什么设置为零?只有在我阅读了接下来的几行之后才有意义。
以下是我将如何重写它:
function mode(arr) {
var modeCount = {};
for (var i = 0; i < arr.length; i++) {
// Save the value so we don't have to do arr[i] every time
var currMode = arr[i];
// If it exists
if (modeCount[currMode]) {
// Increment by 1
modeCount[currMode]++;
} else {
// Otherwise, it's the first one
modeCount[currMode] = 1;
}
}
return modeCount;
}
console.log(mode([3, 4, 3, 43, 4, 34, 34, 3, 3, 3, 3, 3]))
答案 4 :(得分:0)
该函数循环传递的数组,将每个事件编号存储为modecount对象上的number:count对。因此,当您的函数工作时,modecount对象将如下所示:
{}
{ 3: 1 }
{ 3: 1, 4: 1 }
{ 3: 2, 4: 1 }
{ 3: 2, 4: 1, 43: 1 }
{ 3: 2, 4: 2, 43: 1 }
and so on...