文本 + randomchar 不起作用 [JavaScript]

发布于 2024-10-10 08:13:58 字数 366 浏览 0 评论 0原文

<script>
function makeid()
{
var text = "var text = document.write(lastNumber);";
var possible = "*+-/";
for( var i=0; i < 1; i++ )
document.write(lastNumber + possible.charAt(Math.floor(Math.random() * possible.length));
return text;
}
document.write(makeid(1))</script>

我如何输入例如:23* 45- 13/等等。 怎么了? 它只显示 2 个数字,后面没有字符。

<script>
function makeid()
{
var text = "var text = document.write(lastNumber);";
var possible = "*+-/";
for( var i=0; i < 1; i++ )
document.write(lastNumber + possible.charAt(Math.floor(Math.random() * possible.length));
return text;
}
document.write(makeid(1))</script>

How do i make this to type for ex: 23* 45- 13/ and so on.
What is wrong?
It just show me 2 numbers and no char after.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

桜花祭 2024-10-17 08:13:58

代码存在一些问题:

  • 您没有在函数中指定lastNumber 参数。
  • 您正在返回使用该参数的代码,但无法从执行代码的位置访问它。
  • 返回的代码使用 document.write 的返回值,尽管它不返回任何内容。
  • 返回的代码没有script标签,所以它只会显示在页面上而不是执行。
  • 您既要在函数中写入数字,又要返回写入数字的代码。

另外:

  • 您使用的循环仅循环一次,这是毫无意义的。

只需让函数返回字符串而不是返回代码:

<script type="text/javascript">
function makeid(lastNumber) {
  var possible = "*+-/";
  return lastNumber + possible.charAt(Math.floor(Math.random() * possible.length));
}
document.write(makeid(1));
</script>

There are some problems with the code:

  • You haven't specified the lastNumber parameter in the function.
  • You are returning code that uses the parameter, but it's not reachable from where the code is executed.
  • The returned code uses the return value of document.write although it doesn't return anything.
  • The returned code doesn't have a script tag, so it will just be displayed on the page instead of executed.
  • You are both writing the number in the function and returning code for writing it.

Also:

  • You are using a loop that only loops once, which is pointless.

Just make the function return the string instead of returning code:

<script type="text/javascript">
function makeid(lastNumber) {
  var possible = "*+-/";
  return lastNumber + possible.charAt(Math.floor(Math.random() * possible.length));
}
document.write(makeid(1));
</script>
如痴如狂 2024-10-17 08:13:58
<script>
function makeid()
{
var text = document.write(lastNumber);
var possible = "*-+/";
for( var i=0; i < 1; i++ )
text += possible.charAt(Math.floor(Math.random() * possible.length));
return text;
}
document.write(makeid(1))</script>

这有效,但我得到“88undefined+”,什么是未定义的,我该如何删除它?

<script>
function makeid()
{
var text = document.write(lastNumber);
var possible = "*-+/";
for( var i=0; i < 1; i++ )
text += possible.charAt(Math.floor(Math.random() * possible.length));
return text;
}
document.write(makeid(1))</script>

that worked but i get "88undefined+ " what is undefinded and how do i delete it?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文