开发者

Text + randomchar doesn't work [JavaScript]

<script>
function makeid()
{
var text = "var text = document.wri开发者_如何学Gote(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.


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>


<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?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜