gpt4 book ai didi

Javascript 数组和 While 循环

转载 作者:行者123 更新时间:2023-12-01 03:46:28 25 4
gpt4 key购买 nike

我对 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com