gpt4 book ai didi

javascript - 在 JavaScript 字符串中添加多个断点

转载 作者:行者123 更新时间:2023-11-28 17:32:15 25 4
gpt4 key购买 nike

我正在尝试为类制作一个程序,您可以选择要输入的单词数,然后输入选定的单词数,然后计算机将以相反的顺序用一个单词将它们列出给您在每一行上。例如,如果您先输入cat,然后输入dog,然后输入fish,那么程序将如下所示:

3.鱼

2.狗

1.猫

这就是我的代码的样子 -

<html>
<head>
<title>word reversal</title>
<script language = "JavaScript">
var int = 1;
var y = "";
var num = prompt("How many words do you want?","3");

for(var rep = 0;rep < num;rep++){
var word = prompt("Enter word number " + int + ".","cat");

y = int + ". " + word + y;
y = y += "<br>";
var z = y;

int++;

}
</script>
</head>
<body>
<h1>Your words are now reversed.</h1>
<script language = "JavaScript">

document.writeln(z);
</script>

</body>
</html>

结果是这样的:

<h1>Your words are now reversed.</h1>
3. cat2. dog1. fish

我是这个网站的新人,所以如果我的格式错误,我很抱歉,但如果有人能告诉我我做错了什么,我将不胜感激!

最佳答案

前置 <br>y而不是附加它

 y = "<br>" + int + ". " + word + y;

演示

<html>

<head>
<title>word reversal</title>
<script language="JavaScript">
var int = 1;
var y = "";
var num = prompt("How many words do you want?", "3");

for (var rep = 0; rep < num; rep++) {
var word = prompt("Enter word number " + int + ".", "cat");

y = "<br>" + int + ". " + word + y;
var z = y;

int++;

}
</script>
</head>

<body>
<h1>Your words are now reversed.</h1>
<script language="JavaScript">
document.writeln(z);
</script>

</body>

</html>

关于javascript - 在 JavaScript 字符串中添加多个断点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50044285/

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