输出输入字符串10次JavaScript(Output inputted String 10 times JavaScript)

编程入门 行业动态 更新时间:2024-10-07 04:25:59
输出输入字符串10次JavaScript(Output inputted String 10 times JavaScript)

我正在尝试编写一个程序,它将读取用户输入的字符串(单词或句子)。

我用一个按钮调用了一个函数( 但是没有函数的解决方案也会很好,以防万一 )。 程序当前从输入中读取单词,并且根本不输出到屏幕。

我不确定看到它是什么问题(我有点像菜鸟),但没有任何错误需要考虑。

如果可能的话,我希望程序在警报框中输出到屏幕上。

这是我的代码:

<body> <button onclick="outputMessage()">Try it</button> <script> function outputMessage(){ var text = ""; var word; word = prompt("please enter a word"); for (word = '0'; word == '10'; word ++) { text += "The word is " + word + ""; } alert(text); } </script> </body>

感谢您的帮助 :)

-ShadowMinion

I am trying to write a program which will read a string (word or sentence) input from the user.

I have called a function for this with a button (But a solution without a function would be great as well just in case). The program currently reads the word from input and it is not outputting to the screen at all.

I am not sure what the problem is by looking at it (I'm a bit of a noob) but there are no errors to take into account.

I want the program to output to be on screen in an alert box if possible.

Here is my code:

<body> <button onclick="outputMessage()">Try it</button> <script> function outputMessage(){ var text = ""; var word; word = prompt("please enter a word"); for (word = '0'; word == '10'; word ++) { text += "The word is " + word + ""; } alert(text); } </script> </body>

Thank you for your help :)

-ShadowMinion

最满意答案

如果我理解正确,您希望用户输入的单词显示十次?

在那种情况下,你做的大部分都是好的。 只有你的循环不正确。 请参阅此页面以获取一些指导: https : //developer.mozilla.org/nl/docs/Web/JavaScript/Guide/Loops_and_iteration

for (var i = 0; i < 10; i++) { text += "The word is " + word + "\n"; // /n is a new line }

这是一个有效的例子: https : //jsfiddle.net/n6d5wpgz/1/

If I understand it correctly, you want the word the user typed in, to be displayed ten times?

In that case you're doing the most part good. Only your loop is incorrect. See this page for some guidance: https://developer.mozilla.org/nl/docs/Web/JavaScript/Guide/Loops_and_iteration

for (var i = 0; i < 10; i++) { text += "The word is " + word + "\n"; // /n is a new line }

Here's a working example: https://jsfiddle.net/n6d5wpgz/1/

更多推荐

本文发布于:2023-07-27 08:28:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1287740.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字符串   JavaScript   Output   String   inputted

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!