I want to randomly display the characters entered by the user.

Asked 1 years ago, Updated 1 years ago, 23 views

I would like to randomly display the characters entered by the user.
For example, if you type "Fish", "Ni", and "Rice", I would like to create a script that randomly changes the order, such as "Ni-go-han-san-ka" and "Rice-Fish-Niku".
If there are only two entries, I would like to randomly display only those two.

If I were to create a variable for each input item, I think it would be better to display the variable at random, but I don't know how to automatically ignore the blank when there is a blank and display a variable that is not a blank.In the first place, I don't know how to display the contents of variables randomly.

javascript

2022-09-30 11:28

1 Answers

Like a comment said you need to get the input into variables somehow

For simplicity we can use window.prompt, then to select a random one we should store each variable in an area, then to display it where can just use document.body.innerHTML

So

<body>

<Script>
var things = [ ]
varone=prompt("enter one thing")
things.push(one)


one=prompt("enter one other thing")
things.push(one)

one=prompt("enter one more thing")
things.push(one)

/*
Now we loop through it randomly using Math.random and Math.floor
*/
var toytawl=""
for(var index=0;index<things.length;index++){
var found = Math.floor (Math.random() *things.length)
   toytawl+=things [found]
things.splice(found,1)
}
</Script>
</Body>

Translation from Google translate

I was told that for some reason I had to get input into the variable like the comment

To make it easier, simply use window.prompt and select random variables by storing each variable in a region and using document.body.innerHTML to display it.

That's right

<Body>

<Body>
var things = [ ]
varone=prompt ("Please enter one")
things.push(one)


one = prompt ("Please enter one more")
things.push (one)

one = prompt ("Please enter one more")
things.push (one)

/ *
Then loop randomly using Math.random and Math.floor.
* /
var toytawl=""
for (var index=0;index<things.length;index++) {
var found = Math.floor (Math.random() *things.length)
     toytawl=things [found]
things.splice (found, 1)
}
document.body.innerHTML = toytawl
</Script>
</Body>

translation from Google Translation


2022-09-30 11:28

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.