gpt4 book ai didi

javascript - 如何使用变量更改图像高度?

转载 作者:行者123 更新时间:2023-12-03 08:20:27 24 4
gpt4 key购买 nike

我必须创建句子中出现的每个字母的频率计数。然后将该频率放入图表中。我们采用这个 1px x 1px gif 并将其拉伸(stretch)以达到当前字母频率的标准化值(最大 100px)。例如,如果我有短语“Cwm fjordbank 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 的值。有人可以解释一下我该怎么做吗?

最佳答案

看起来串联已关闭。改变这个

'<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>';

请注意,连接的每个字符串均由单引号 ' 分隔。

关于javascript - 如何使用变量更改图像高度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33768231/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com