所以我有一个任务,我正在努力,我不能为我的生活弄清楚如何完成最后一部分。分组整数数组
我有一个数组数组,我必须对这些数字进行排序,然后我必须将它们分组,每个数字有多少个。那是我陷入困境的一部分。
我做的这一切在一个文档中为这项任务:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<link href="https://fonts.googleapis.com/css?family=Lato" rel="stylesheet">
<title>Arrays For Days</title>
<style>
body {
line-height: 1;
font-family: 'Lato', sans-serif;
letter-spacing: 0.08em;
}
p.headings {
font-style:italic;
font-size: 20px;
}
</style>
</head>
<body>
<h1 style="text-decoration: underline;">Sorting and Grouping Arrays</h1>
<br>
<p class="headings">Starting List of Numbers:</p>
<p id="startNumbers"></p>
<br>
<p class="headings">Numbers Sorted:</p>
<p id="arraySorted"></p>
<br>
<p class="headings">Array Sorted and then Grouped:</p>
<p id="arrayGrouped"></p>
<script>
var numbers = [1, 5, 17, 4, 9, 3, 1, 17, 32, 5, 3, 27, 9, 18, 3, 12, 67, 18, 32, 1, 19, 21, 1, 17];
//printed original numbers to HTML element using document.getElementById
document.getElementById("startNumbers").innerHTML = numbers;
//used .sort method, with a comparative function using a-b, so if a is less than b then the result...
//should be a sorting of lesser to greater.
numbers.sort (
function(a, b) {
return a - b;
});
//then I printed the now sorted numbers to matching HTML element using document.getElementById
document.getElementById("arraySorted").innerHTML = numbers;
document.getElementById("arrayGrouped").innerHTML = output;
</script>
</body>
</html>
只是试图让数字来显示我的网页是这样的:
1:4,3:3, 4:1,5:2,9:2,12:1,17:3,18:2,19:1,21:1,27:1,32:2,67:1
这是可能的但我一直只找到不像这样的例子,并且不得不处理一些其他与数组相关的东西,我不需要这个任务。谢谢你的人的帮助:-)
弥敦道L.
如果你能找到一种方法来排序,而无需使用数组,你会赢得所有的代码。 –
是的,不幸的是我必须这样做:-( –