首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在for循环中添加设定次数的数字并在HTML上显示

如何在for循环中添加设定次数的数字并在HTML上显示
EN

Stack Overflow用户
提问于 2020-09-01 18:29:15
回答 1查看 85关注 0票数 0

**嗨,对于一些看起来很简单的问题,我很抱歉,因为我对Javascript编码非常陌生。在这里,我有三个输入字段:风管大小,隔热层和孔数。我想在HTML上显示与孔数量、隔热层和输入的风管大小相关的测量结果数组。例如第一孔测量、第二孔测量、第三孔测量等。但是,第一个测量值的计算方式与其余测量值的计算方式不同。也许这可以澄清公式和我想要在屏幕上显示的内容:

1.在风管尺寸中输入测量值。

2.输入隔热层厚度。

3.输入孔数。有了这些信息,我想根据孔数和风管尺寸测量值来计算和显示测量值。

下面是一个示例:

风管尺寸: 400 mm

绝缘层: 50 mm

孔数:4

隔热层(50)*2-风管尺寸例如400 - 100 = 300然后300 /4(孔数)= 75 (希望在HTML上将此数字显示为:“孔间距= 75 mm ")

对于第一个测量值(第一个孔间距)=(孔间距)75 /2 + 50 (绝缘层)= 87.5 mm(这将是在DOM上显示的第一个测量值)。

然后,对于所有其他相对测量(由孔数>元素确定),将孔间距添加到第一个孔间距,并显示此信息。

例如87.5 + 75 = 162.5 (这是显示的第二个孔测量值)

162.5 + 75 = 237.5 (这是显示的第三个孔测量值)

237.5 + 75 = 312.5 (这是显示的第三个孔尺寸)**

代码语言:javascript
复制
    function ductPitotHoleCalculate() {
    var ductSize = document.getElementById("duct-size").value;
    var insulation = document.getElementById("insulation").value;
    var amountOfHoles = document.getElementById("number-of-holes").value;

    //It should multiply insulation by 2 and subtract from ductsize

    var subtractInsulation = parseFloat(ductSize) - parseFloat(insulation) * 2;

    //It should find the measurement between holes by dividing numberofHoles and subtractInsulation.

    var spacingOfHoles = subtractInsulation / parseFloat(amountOfHoles);

    //It should use the spacingOfHoles number and divide by 2 and add insulation for first hole and show on DOM 

    var firstHoleSpacing = spacingOfHoles / 2 + parseFloat(insulation);


    // It should use a for loop to parse amountOfHoles and add spacingofHoles to firstHoleSpacing

    var newAmount = parseFloat(amountOfHoles) - 2;
    var myArray = [];
    for (var i = 0; i < newAmount; i + spacingOfHoles) {
        myArray.push(firstHoleSpacing + spacingOfHoles);
    }
    document.getElementById("answer-5").innerHTML = myArray;
    
}
    <section id = "VSD-calculator">
    <div id = "pitot-holes">
    <h2> Calculate pitot hole duct measurements (type 0 for no insulation) 
    </h2>
    <h2 id="answer-5"></h2>
    <input type = "number" id = "duct-size" placeholder="Duct size in 
    mm"class="mytext">
    <input type = "number" id = "insulation" placeholder="Insulation in 
    mm"class="mytext">
    <input type = "number" id = "number-of-holes" placeholder="Number of 
    Holes"class="mytext">
    <button onclick="ductPitotHoleCalculate()"id="calc-button" class = "calc" 
    >Calculate</button>
    </div>
    </section>
EN

回答 1

Stack Overflow用户

发布于 2020-09-02 17:27:44

感谢所有帮助我的人,我现在已经解决了这个问题!我认为,通过解释并输入它,帮助我理解了它并编写了代码。我最终改用了while循环。

代码语言:javascript
复制
    function ductPitotHoleCalculate() {
    var ductSize = document.getElementById("duct-size").value;
    var insulation = document.getElementById("insulation").value;
    var amountOfHoles = document.getElementById("number-of-holes").value;

    //It should multiply insulation by 2 and subtract from ductsize
    var subtractInsulation = parseFloat(ductSize) - parseFloat(insulation) * 2;

    //It should find the measurement between holes by dividing numberofHoles and 
    subtractInsulation.
    var spacingOfHoles = subtractInsulation / parseFloat(amountOfHoles);
    //It should use the spacingOfHoles number and divide by 2 and add insulation for 
    first 
    hole and show on DOM 
    var firstHoleSpacing = spacingOfHoles.toFixed(2) / 2 + parseFloat(insulation);
    var i = 0;
    var strHoles = parseFloat(amountOfHoles);
    var myArray = '';
    while (i < strHoles) {
        myArray += firstHoleSpacing + (i * spacingOfHoles) + ' mm, ' + '<br />';
        i++;
    }
    document.getElementById("answer-5").innerHTML = `Hole spacing = ${spacingOfHoles} 
    mm    
    ` + '<br />'  + myArray;

    }
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63685911

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档