javascript 中出现次数最多
Highest occurrence in javascript
您好,我对 javascript 的了解非常有限且基础。基本上下面是我会提示一个弹出窗口,显示值的答案。问题来自我在下面找到的编码如果我必须插入一个数组让我们说 1,2,3,2
输出将是 ,
因为它在数组中出现次数最多。有没有办法编辑此代码,以便上面输入的答案为 2
我已经完成了应有的研究:
链接如下:
- Get the element with the highest occurrence in an array
- https://www.sitepoint.com/community/t/numbers-only-input-values-in-text-box-how/3029
代码:
<script type="text/javascript">
function evaluate() {
var input = prompt("Please enter your input");
var array = new Array();
function mode(array)
{
if(array.length == 0)
return null;
var modeMap = {};
var maxEl = array[0], maxCount = 1;
for(var i = 0; i < array.length; i++)
{
var el = array[i];
if(modeMap[el] == null)
modeMap[el] = 1;
else
modeMap[el]++;
if(modeMap[el] > maxCount)
{
maxEl = el;
maxCount = modeMap[el];
}
}
return maxEl;
}
document.writeln("Your calculation is: ");
document.writeln(mode(input) + " with a starting input string of: " + input);
}
</script>
<script type="text/javascript">
evaluate();
</script>
您想先将字符串 '1,2,3,2'
转换为数组 [ 1, 2, 3, 2 ]
。这可以用 split
function. You probably also want to trim
每个元素来完成,以防有人用 space.
格式化它们
function evaluate() {
const input = prompt("Please enter the array of integers")
.split(',')
.map(item => item.trim());
function mode(items) {
const counts = items
.reduce((counts, item) => {
const currentItemCount = counts.get(item) || 0;
return counts.set(item, currentItemCount + 1);
}, new Map());
const maxEntry = Array.from(counts.entries())
.reduce((maxEntry, entry) => {
return entry[1] > maxEntry[1] ? entry : maxEntry;
});
return maxEntry[0];
}
document.writeln("Your calculation is: ");
document.writeln(mode(input) + " with a starting input string of: " + input);
}
evaluate();
您的问题源于您从未将输入(您从 prompt
收到的字符串形式的输入)转换为实际数组。
当直接在字符串上调用 mode
时,逗号作为最常见的字符返回,因为逗号 是 字符串中最常见的字符。
要解决此问题,您需要将字符串转换为实际的数组,以便对数组的元素而不是字符串的字符进行操作。
您可以使用 split
函数将逗号处的字符串 ("1,2,3,2"
) 拆分为一个数组 (["1", "2", "3", "2"]
),然后您可以将其传递给模式函数:
mode(input.split(","))
您好,我对 javascript 的了解非常有限且基础。基本上下面是我会提示一个弹出窗口,显示值的答案。问题来自我在下面找到的编码如果我必须插入一个数组让我们说 1,2,3,2
输出将是 ,
因为它在数组中出现次数最多。有没有办法编辑此代码,以便上面输入的答案为 2
我已经完成了应有的研究:
链接如下:
- Get the element with the highest occurrence in an array
- https://www.sitepoint.com/community/t/numbers-only-input-values-in-text-box-how/3029
代码:
<script type="text/javascript">
function evaluate() {
var input = prompt("Please enter your input");
var array = new Array();
function mode(array)
{
if(array.length == 0)
return null;
var modeMap = {};
var maxEl = array[0], maxCount = 1;
for(var i = 0; i < array.length; i++)
{
var el = array[i];
if(modeMap[el] == null)
modeMap[el] = 1;
else
modeMap[el]++;
if(modeMap[el] > maxCount)
{
maxEl = el;
maxCount = modeMap[el];
}
}
return maxEl;
}
document.writeln("Your calculation is: ");
document.writeln(mode(input) + " with a starting input string of: " + input);
}
</script>
<script type="text/javascript">
evaluate();
</script>
您想先将字符串 '1,2,3,2'
转换为数组 [ 1, 2, 3, 2 ]
。这可以用 split
function. You probably also want to trim
每个元素来完成,以防有人用 space.
function evaluate() {
const input = prompt("Please enter the array of integers")
.split(',')
.map(item => item.trim());
function mode(items) {
const counts = items
.reduce((counts, item) => {
const currentItemCount = counts.get(item) || 0;
return counts.set(item, currentItemCount + 1);
}, new Map());
const maxEntry = Array.from(counts.entries())
.reduce((maxEntry, entry) => {
return entry[1] > maxEntry[1] ? entry : maxEntry;
});
return maxEntry[0];
}
document.writeln("Your calculation is: ");
document.writeln(mode(input) + " with a starting input string of: " + input);
}
evaluate();
您的问题源于您从未将输入(您从 prompt
收到的字符串形式的输入)转换为实际数组。
当直接在字符串上调用 mode
时,逗号作为最常见的字符返回,因为逗号 是 字符串中最常见的字符。
要解决此问题,您需要将字符串转换为实际的数组,以便对数组的元素而不是字符串的字符进行操作。
您可以使用 split
函数将逗号处的字符串 ("1,2,3,2"
) 拆分为一个数组 (["1", "2", "3", "2"]
),然后您可以将其传递给模式函数:
mode(input.split(","))