我必须创建一个句子中出现的每个字母的频率计数。那个频率不是放在图表中。我们将这个1px乘以1px gif并拉伸它以达到当前字母频率的标准化值(最大100px)。例如,如果我有短语“Cwm fjord bank glyphs vext quiz”,那么每个字母都会有100px,因为它只使用每个字母一次。
所以我创建了一个数组来对每个单词的出现时间进行排序。然后找出出现次数最多的单词,以便我知道将每个单词划分为什么。当我制作时,该值将是图像的高度。
function htmlChart() {
var table = document.getElementById("table");
input = document.getElementById("userInput").value;
table.innerHTML = generateTable(input);
}
function generateTable(input) {
var frequency = new Array(26);
var letters = new Array(26);
var freqPos = 0;
var newInput = input.toUpperCase();
var max = 0;
var myHeight = 0;
var test = 9000;
var image = new Image();
image.src = "orange.gif";
for (i = 65; i < 91; i++) {
//looks at how many times each character occurs and stores its value
frequency[freqPos] = newInput.split(String.fromCharCode(i)).length -
1;
freqPos++;
}
//checks which letter occured the most
for (i = 0; i < frequency.length - 1; i++) {
if (frequency[i] > max) {
max = frequency[i];
}
}
table = input + "<table>";
//first row
table += "<tr>";
table += "<td>Letter Frequency 100px</td>";
for (i = 0; i < frequency.length - 1; i++) {
//somehow have to use myHeight to change the height of the image that I make here.
myHeight = (frequency[i] / max) * 100;
table +=
'<td><img src = "orange.gif" id = "orange" alt = "25" height = myHeight + "px" width = "5"></td>';
}
table += "</tr>";
//second row
table += "<tr>";
table += "<td></td>";
for (i = 65; i < 91; i++) {
table += "<td>" + String.fromCharCode(i) + "</td>";
}
table += "</tr>";
table += "</table>";
return table;
}
所以你可以看到我已经制作了频率图表,但我只是不知道如何将高度更改为myHeight的值。有人可以解释我将如何做到这一点吗?
答案 0 :(得分:1)
看起来连接已关闭。改变这个
'<td><img src = "orange.gif" id = "orange" alt = "25" height = myHeight + "px" width = "5"></td>';
到
'<td><img src = "orange.gif" id = "orange" alt = "25" height="' + myHeight + 'px" width = "5"></td>';
请注意,连接的每个字符串都由单引号'
分隔。