You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.02 13:23:24