我正在try 编写一个JavaScript程序,它将接受用户输入,即他们的吉他使用的音阶长度,以及它有多少基调,然后进行必要的计算,以解决每个基调之间的距离.
我希望它的工作方式是,变量将根据需要创建.因此,用户输入他们的音量和音量,程序将显示一个包含所有内容的HTML表,并且只显示他们最初输入的音量的行.
问题是,我对JavaScript完全陌生,除了了解for循环之外,我对如何实现这一点一无所知.
编辑:这些值现在计算正确了,也正确地进入了表中!以下是我的更新代码:
<!DOCTYPE html>
<html>
<head>
<title>Banjo Neck Calculations</title>
<link rel="stylesheet" href="styles/styles.css">
</head>
<body>
<table>
<tbody id="scaleLengthTable">
<tr>
<th colspan="2" id="tableHeader"></th>
</tr>
<tr>
<th>Fret</th>
<th>Distance From Nut</th>
</tr>
</tbody>
</table>
<script> // Calculates the the scale length
let scaleLength = prompt("What is your desired scale length?"); // Prompts user for the scale length of their instrument
let numFrets = prompt("How many frets will you have?"); //Prompts user for the number of frets on their instrument
let testing = 0; // Placeholder value, updated by the below for loop
let distFromNut = 0; // Placeholder value, updated by the below for loop
const fretDists = []; // An array that is filled with values by the below for loop
let main_table = document.getElementById("scaleLengthTable");
for (let i = 0; i < numFrets; i++) {
// Create new row and cells for the table
let row = document.createElement("tr");
let cell1 = document.createElement("td");
let cell2 = document.createElement("td");
testing = (scaleLength - distFromNut) / 17.817;
fretDists.push(testing);
distFromNut = fretDists[i] + distFromNut;
// Add the cells to the row and the row to the table body
cell1.appendChild(document.createTextNode(i + 1));
cell2.appendChild(document.createTextNode(distFromNut.toFixed(4) + '"'));
row.appendChild(cell1);
row.appendChild(cell2);
main_table.appendChild(row);
document.getElementById("tableHeader").innerHTML = "Scale Length = " + scaleLength + '" - ' + numFrets + " Frets";
}
</script>
</body>
</html>