在用户输入段落后,javascript将计算每个单词appreas的频率并将其从最大到最小打印出来。我已经通过使用两个数组来计算每个单词的数量。但是,我真的不知道如何根据数量将它们打印出来,因为计数和单词存储在两个不同的数组中。请帮我。谢谢!
innerHTML
答案 0 :(得分:1)
你可以试试这个:
1-插入您的数据:单词和单词在同一个对象中计数
2-使用键count
<p id="demo"></p>
<p id="result"></p>
<input type="text" id="paragraph" class="field",>
<button onclick="input()"><b>Submit</b></button>
<script>
function input() {
var test = document.getElementById("paragraph").value;
var arr =[];
arr = test.toLowerCase().replace(/[^a-zA-Z ]/g, "").split(/\s+/);
/*remove all special characters: there remove anything that's not letter*/
/*or replace(/[&\/\\#,+()$~%.'":*?<>{}]/g, '') --> place all characters you want to avoid here */
document.getElementById("demo").innerHTML = arr;
function foo(arr) {
var a = [];
var new_item;
var counter=0;
for ( var i = 0; i < arr.length; i++ ) {
new_item=true;
for (var j = 0; j < a.length; j++) {
if(a[j]['word']== arr[i]){
a[j]['count'] ++;
new_item=false;
}
}
if(new_item){
a[counter] = {}; a[counter]["word"] = arr[i];a[counter]["count"] = 1;
counter ++;
}
}
a.sort(function(a,b) {
return parseInt(b.count) - parseInt(a.count);
});
return a;
}
var result = foo(arr);
for ( var i = 0; i < result.length; i++ ) {
document.write("you wrote :"+result[i]['word']+" "+result[i]['count']+" times<br/>");
}
}
</script>
&#13;
答案 1 :(得分:0)
通过按降序对数组进行排序可以很容易地实现,因为数组b中的所有元素都与数组a中的元素相关。例如: - [aaa,你好] [1,3]表示“aaa”的1个频率和“hello”的3个频率。如果按降序对B数组进行排序,它将为[3,1],如果在同一循环中交换数组a的所有值,它将解决问题。
<!DOCTYPE html>
<html>
<head>
<title> practice </title>
<style>
.field{
font-size: 10px; /
width: 400px;
}
</style>
</head>
<body>
<p> This progam will calculate what you want to</p>
<p id="demo"></p>
<p id="result"></p>
<input type="text" id="paragraph" class="field",>
<button onclick="input()"><b>Submit</b></button>
<script>
function input() {
var test = document.getElementById("paragraph").value;
var arr =[];
arr = test.toLowerCase().split(" ");
document.getElementById("demo").innerHTML = arr;
function foo(arr) {
var a = [], b = [], prev;
arr.sort();
for ( var i = 0; i < arr.length; i++ ) {
if ( arr[i] !== prev ) {
a.push(arr[i]);
b.push(1);
} else {
b[b.length-1]++;
}
prev = arr[i];
}
//Code modification by Dixit starts
//Sorting the arrays in descending order
for(i=0;i<b.length; i++)
{
for(j=i+1;j<b.length; j++)
{
if(b[i] < b[j])
{
var temp = b[j];
b[j] = b[i];
b[i]= temp;
var temp1 = a[j];
a[j] = a[i];
a[i]= temp1;
}
}
}
//Sorting the arrays in descending order
//Code modification by Dixit ends
return [a, b];
}
var result = foo(arr);
document.write('[' + result[0] + ']<br>[' + result[1] + ']')
}
</script>
</body>
</html>
请注意,我仅使用冒泡排序技术作为示例,最坏情况时间复杂度为0(N ^ 2)。您可以根据输入的数量使用任何排序技术。
答案 2 :(得分:0)
我尝试使用Array map
var test = "a a b c a 4 3 fff 3 9 fff 9 bb"
var arr =[];
arr = test.toLowerCase().split(" ");
function sortProperties(obj)
{
// convert object into array
var sortable=[];
for(var key in obj)
if(obj.hasOwnProperty(key))
sortable.push([key, obj[key]]); // each item is an array in format [key, value]
// sort items by value
sortable.sort(function(a, b)
{
var x=a[1],
y=b[1];
return x>y ? -1 : x<y ? 1 : 0;
});
return sortable; // array in format [ [ key1, val1 ], [ key2, val2 ], ... ]
}
function calculate(myArr) {
var result = {};
myArr.map(function(item){
if (!result[item]){
result[item]=1;
}
else {
result[item]++;
}
},result);
return sortProperties(result);
}
var result = calculate(arr);
console.log(result);