Javascript 数组和 While 循环

标签 javascript arrays while-loop

我对 javascript 很陌生,我正在尝试在简历网站上列出一定数量的工作职责(由用户输入决定)。例如,我要求用户输入他们想要查看的工作职责数量,并使用数组和 while 循环来显示这么多工作职责。但是,当我单击按钮时,就会发生注意到。我根本没有收到任何网络控制台错误。这是我到目前为止所拥有的:

<div id="right">
            <p> <b> Byrne Dairy</b><br/>
            QA Lab Technician<br/>
            September 2015 - March 2016<br/><br/><br/>

            <button value="Click" onclick="listDuties()">Click</button> to see my top
                <input type="text" id="byrne_duties" value="0"/> job duties here:<br/><br/><br/>
            <p id="duties"></p>
            <script type="text/javascript">
            function listDuties() {
            var byrneduties = [     "Gathering product samples from sanitized silo and tank ports on a timely schedule. <br/> ",
                                    "Regularly use of Delta Testing Instruments to measure solids and fat contents of products. ",
                                    "Performing the Gerber Method of testing on samples. ",
                                    "Responsible for using the Standard Plate Count method of developing colonies of bacteria. ",
                                    "Interpreting results of bacterial and coliform growth patterns in products. " ];
            var x = byrneduties[0];
            var text = "";
            while (byrneduties[x]) {
            text += byrneduties[x] + "<br>";
            x++;
            document.getElementById('duties').innerHTML = x;
            }
            }
            </script>


        </div>

有人告诉我尝试从用户输入中减去一,但我不知道该怎么做。任何帮助都会很棒!

最佳答案

哦,您的代码中有一些错误:

  • 首先,您的 while 条件不是 bool 值(true/false),而是字符串数组的值
  • 在循环中用作索引的 var x 使用字符串数组的第一个元素进行初始化,然后递增(字符串+1?!),最后返回到 html 对象( p)

查看以下经过审查的代码,我在其中进行了上述小更改:

function listDuties() {
  var byrneduties = ["Gathering product samples from sanitized silo and tank ports on a timely schedule. <br/> ","Regularly use of Delta Testing Instruments to measure solids and fat contents of products. ","Performing the Gerber Method of testing on samples. ","Responsible for using the Standard Plate Count method of developing colonies of bacteria. ","Interpreting results of bacterial and coliform growth patterns in products. " ];
  
  var n = document.getElementById('byrne_duties').value;
  var x = 0;
  var text = "";
  while (x < n) {
    text += byrneduties[x] + "<br>";
    x++;
  }
  document.getElementById('duties').innerHTML = text;
}
<div id="right">
    <p> <b> Byrne Dairy</b><br/>
    QA Lab Technician<br/>
    September 2015 - March 2016<br/><br/><br/>

    <button value="Click" onclick="listDuties()">Click</button> to see my top
        <input type="text" id="byrne_duties" value="0"/> job duties here:<br/><br/><br/>
    <p id="duties"></p>

</div>

我希望这是清楚的,再见。

关于Javascript 数组和 While 循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43543940/

相关文章:

javascript - 无法通过javascript函数更改css属性

c++ - 将二维数组传递给函数而不指定第二维时出现编译器错误

c - 如何对结构数组进行qsort

swift - 使用 while 循环和 swift 中的 break 关键字显示从 1 到 500 的偶数

Javascript/HTML 语法格式错误

javascript - 从 div Angular 删除内容

r - For 语句嵌套在 while 语句中不起作用

php - 将值添加到数组中

javascript - 使用 ionic : Array return value 0 为 YouTube channel 构建混合移动应用程序

javascript - 如何使用数组添加撤消列表?