作业要求而不是显示编号25,75,125,175,页面上应显示单词。我已经设置了if语句,但由于某种原因,我无法摆脱25,75,175的数量。请帮忙!
提前致谢。
作业2
<script>
//start of Assignment 2-1
var new_text = "";
var randMax = (Math.floor(Math.random() * 200));
for (var i = 1; i < randMax; i++) {
new_text += i;
if (i == 25) {
new_text += "Twenty Five ";
} else if (i == 75) {
new_text += "Seventy Five ";
} else if (i == 125) {
new_text += "one hundred and twenty five ";;
} else if (i == 175) {
new_text += "one hundred and Seventy five ";
} else {
new_text += " ";
}
}
document.write(new_text);
</script>
答案 0 :(得分:1)
在for循环的开头,您使用new_text += i;
。这意味着它将始终将i附加到您的new_text
字符串。如果你想让它成为条件,你需要把它放在else语句中。
<script>
//start of Assignment 2-1
var new_text = "";
var randMax = (Math.floor(Math.random() * 200));
for (var i = 1; i < randMax; i++) {
if (i == 25) {
new_text = "Twenty Five ";
} else if (i == 75) {
new_text += "Seventy Five ";
} else if (i == 125) {
new_text += "one hundred and twenty five ";;
} else if (i == 175) {
new_text += "one hundred and Seventy five ";
} else {
new_text += i;
new_text += " ";
}
}
document.write(new_text);
</script>
答案 1 :(得分:0)
var new_text = "";
var randMax = (Math.floor(Math.random() * 200));
for (var i = 1; i < randMax; i++) {
if (i == 25) {
new_text += "Twenty Five ";
} else if (i == 75) {
new_text += "Seventy Five ";
} else if (i == 125) {
new_text += "one hundred and twenty five ";;
} else if (i == 175) {
new_text += "one hundred and Seventy five ";
} else {
new_text += i;
}
new_text += " ";
}
alert(new_text);
将“new_text + = i”移动到else块。现在无论条件如何,它都会在每次迭代时运行