【发布时间】:2021-02-24 20:50:09
【问题描述】:
背景:我一直在做嵌套 for 循环练习。我正在编写的程序旨在获取列和行数据并使用嵌套的 for 循环创建字母矩阵或网格。我能够将数据编译到网格中(太棒了!),但是在网格顶部有一行不必要的数据。(不太好......)谁能让我知道可能导致这种情况的原因,我相信这是非常简单的事情,但我的大脑可能因为看了这么久而被烧毁了。
任何帮助将不胜感激。
<!DOCTYPE html>
<html lang="en">
<head>
<title>""</title>
<script>
function letter_matrix() {
let column = parseInt(document.form.letter_column.value);
let rows = parseInt(document.form.letter_rows.value);
let text = "<table><th></th>";
let letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZ";
for (let i = 0; i < rows ; ++i) {
text += "<tr>"+ letters.charAt(i) + "</td>";
for(let j = 0; j < column ; j++){
let k = (i * rows + j) % 26;
text += "<td>"+letters.charAt(k)+ "</td>";
}
}
text += "</table>";
document.getElementById("results").innerHTML= text;
}
</script>
</head>
<body>
<h1>""</h1>
<form name="form" action = "javascript:letter_matrix();">
Columns: <input type="text" name="letter_column">
Rows: <input type="text" name="letter_rows">
<input type="submit" value="Enter values.">
</form>
<pre id="results">
</pre>
</body>
</html>
【问题讨论】:
-
你是这个意思吗?
""
-
不,他的意思是渲染的矩阵中还有另一行
-
其实,你应该总是把
标签: javascript for-loop matrix nested-loops letter