如何用FOR循环计算乐器弦长并生成HTML品距表格?
吉他/班卓琴品距计算程序实现
我想编写一个JavaScript程序,接收用户输入的乐器有效弦长(Scale Length)和品数,计算每个品到琴枕(Nut)的距离,最终生成仅包含对应品数行数的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>
内容的提问来源于stack exchange,提问作者hjlemon02
相关产品推荐
相关产品推荐

