Java-脚本根据用户输入创建列和行
Java-script create columns and rows based on user input
这是一个 JavaScript 家庭作业问题,我花了太长时间在网上搜索才弄明白。基于学习循环,while、do while 和 for 循环。我要:
Prompt the user for a number of rows
and a number of columns. Using the * output the *
character in the row and column pattern requested
by the user. If the user requests 2 rows and 2
columns you should output:
**
**>
到目前为止的代码:
\\ var row = prompt("多少行?")
var column = prompt("有多少列?") \\
如果能给我找到答案的指导,我将不胜感激。
鉴于您提到输出是table的形式,我们在这里使用两个循环。第一个是追加每一行,嵌套循环是追加每一列。 colData.innerHTML = "*"
给出应该包含在每一列中的 value/pattern。
在 html 文件中,整个 table 被附加到具有 pattern
作为其 ID 属性的元素中。
let table = document.createElement('table');
let tbody = document.createElement('tbody');
table.appendChild(tbody);
document.getElementById('pattern').appendChild(table);
var row = prompt("How many rows?");
var column = prompt("How many columns?");
for(let i = 0 ; i< row; i++){
let rowVal = document.createElement('tr');
for(let j = 0; j< column; j++){
let colData = document.createElement('td');
colData.innerHTML = "*";
rowVal.appendChild(colData);
}
tbody.appendChild(rowVal);
}
这是一个 JavaScript 家庭作业问题,我花了太长时间在网上搜索才弄明白。基于学习循环,while、do while 和 for 循环。我要:
Prompt the user for a number of rows and a number of columns. Using the * output the * character in the row and column pattern requested by the user. If the user requests 2 rows and 2 columns you should output:
**
**>
到目前为止的代码:
\\ var row = prompt("多少行?") var column = prompt("有多少列?") \\
如果能给我找到答案的指导,我将不胜感激。
鉴于您提到输出是table的形式,我们在这里使用两个循环。第一个是追加每一行,嵌套循环是追加每一列。 colData.innerHTML = "*"
给出应该包含在每一列中的 value/pattern。
在 html 文件中,整个 table 被附加到具有 pattern
作为其 ID 属性的元素中。
let table = document.createElement('table');
let tbody = document.createElement('tbody');
table.appendChild(tbody);
document.getElementById('pattern').appendChild(table);
var row = prompt("How many rows?");
var column = prompt("How many columns?");
for(let i = 0 ; i< row; i++){
let rowVal = document.createElement('tr');
for(let j = 0; j< column; j++){
let colData = document.createElement('td');
colData.innerHTML = "*";
rowVal.appendChild(colData);
}
tbody.appendChild(rowVal);
}