Javascript 需要澄清我写的这段代码是为了找到数组的模式吗
由于某些原因,我无法理解这段代码中发生了什么Javascript 需要澄清我写的这段代码是为了找到数组的模式吗,javascript,Javascript,由于某些原因,我无法理解这段代码中发生了什么 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 t
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]);
功能模式(arr){
var modecount={};//创建对象以添加编号及其模式
对于(var i=0;i
看起来我已经盯着这个代码看了好几个小时了,就是弄不懂。请有人像我3岁一样向我解释这一点。它基本上是计算传递数组中某个特定值存在的时间,但第一次出现该值时,基值为0,以该值初始化属性 即使将
modecount
指定为对象,也可以使用括号表示法以可变方式访问对象的属性。那么
mode([3,4,3,43,4,34,34,3,3,3,3,3]);
将是:
{ 3: 7, 4: 2, 43: 1, 34: 2 }
它基本上是计算传递的数组中某个特定值存在的时间,但该值第一次出现时的基值为0,以将属性初始化为该值 即使将
modecount
指定为对象,也可以使用括号表示法以可变方式访问对象的属性。那么
mode([3,4,3,43,4,34,34,3,3,3,3,3]);
将是:
{ 3: 7, 4: 2, 43: 1, 34: 2 }
我假设第一部分只是简单地检查
modecount[arr[I]]
是否已经初始化/定义。如果它返回一个“falsey”值,则它要么未初始化(访问modecount[arr[i]]
返回未定义的值),要么有一个0值-后者不可能,因为下一行总是立即将0增加到1
注意:您可以使用if(typeof(modecount[arr[i]])==“undefined”)
替换此检查,以获得等效和更明确的行为
关于函数的整体操作,请考虑下面的伪代码:
var modecount = {}
foreach element in array:
if modecount[element.value] is not defined:
modecount[element.value] = 0
modecount[element.value]++
我假设第一部分只是简单地检查modecount[arr[I]]
是否已经初始化/定义。如果它返回一个“falsey”值,则它要么未初始化(访问modecount[arr[i]]
返回未定义的值),要么有一个0值-后者不可能,因为下一行总是立即将0增加到1
注意:您可以使用if(typeof(modecount[arr[i]])==“undefined”)
替换此检查,以获得等效和更明确的行为
关于函数的整体操作,请考虑下面的伪代码:
var modecount = {}
foreach element in array:
if modecount[element.value] is not defined:
modecount[element.value] = 0
modecount[element.value]++
函数在传递的数组中循环,将每个事件编号存储为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...
函数在传递的数组中循环,将每个事件编号存储为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...
我希望这有帮助。我重新编写了几个变量,因为我觉得它读起来更好一些
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]);
功能模式(arr){
//为我们的计数器创建索引
var指数={};
//循环遍历传入的数字的法线
对于(var i=0,num;i我希望这会有所帮助。我重新编写了几个变量,因为我觉得它读起来更好一些
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]);
功能模式(arr){
//为我们的计数器创建索引
var指数={};
//循环遍历传入的数字的法线
对于(var i=0,num;i这是一个高级解释:
这是一个数字列表,它计算每个数字出现在列表中的次数
现在,以下是内联解释:
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]);
功能模式(arr){
//初始化对象文字
var modecount={};
对于(变量i=0;i
我认为这个逻辑虽然正确,但可以改进。第一个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]))
功能模式(arr){
var modeCount={};
对于(变量i=0;i
以下是高层解释:
这是一个数字列表,它计算每个数字出现在列表中的次数
现在,她