文字+ randomchar不起作用[JavaScript] [英] Text + randomchar doesn't work [JavaScript]

查看:87
本文介绍了文字+ randomchar不起作用[JavaScript]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<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个数字,之后不显示任何字符.

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.

推荐答案

代码存在一些问题:

  • 您尚未在函数中指定lastNumber参数.
  • 您将返回使用该参数的代码,但是无法从执行代码的地方到达.
  • 返回的代码使用document.write的返回值,尽管它不返回任何内容.
  • 返回的代码没有脚本标签,因此将仅显示在页面上而不执行.
  • 您既要在函数中写入数字,又要返回用于编写它的代码.
  • 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.

也:

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

只需使函数返回字符串而不是返回代码即可:

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>

这篇关于文字+ randomchar不起作用[JavaScript]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆