[英]Javascript: Times table, make first cell blank, trying to get this output
這是我在給定用戶輸入的情況下創建表格的代碼:
const err = "the valid input is a number between 5 and 20, please refresh your page to start again"; const q1 = "Enter a value"; var user = prompt(q1); var x = parseInt(user, 10); if (Number.isInteger(x) && x >= 5) { x = x; if (x >= 20) { document.getElementById("rer").innerHTML = err; } } else { document.getElementById("rer").innerHTML = err; } function timestable(x) { var r, s; document.write("<table id = 'table' border = 1") for (r = 1; r <= x; r++) { document.write("<tr>") for (s = 1; s <= x; s++) { document.write("<td>" + r * s + "</td>") } document.write("</tr>") } document.write("</table>") } console.log(timestable(x));
這是輸入 10 時的輸出:
我希望 1 當前占用的第一個單元格為空白。 我也希望在未來的cel l至有權從1開始,我想它下面的下一個單元格也從1開始,然后繼續格局。
有什么方法可以將此修復程序應用於我當前的代碼?
您只需要更改您的兩個for
循環以從 0 開始,並更改您在單元格中打印的內容:
for (r = 0; r <= x; r++) {
document.write("<tr>")
for (s = 0; s <= x; s++) {
if (s === 0 && r === 0) {
product = '';
} else if (s === 0) {
product = r;
} else if (r === 0) {
product = s;
} else {
product = r * s;
}
document.write("<td>" + product + "</td>")
}
}
在這個小提琴上看到它
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.