Click here to Skip to main content
15,881,089 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I just started learning JavaScript and I'm new to it. I have this HTML. What this do is, first select how many groups that would be created then input names on the first textarea, then after i click the random button it will automatically distribute the names that you input on the textarea into the groups that would be created. The names are separated by a text splitter which is a comma. Please help guys :( thanks a bunch.

HERE IS THE CODE

HTML
<!DOCTYPE html>
<meta charset="UTC-8">
<html>
	<script>
		//javascript goes here
	</script>
<head>
	
</head>

<body>

	<h2>Add a Group</h2>

<hr>
	
	<table>
		<td>
			<form id="grp">
				Groups:
				<br>
				<input type="number" name="grpmembers" min="1" max="10" id="grps">
			</form>
		</td>
	</table>

	<textarea id="inputNames" rows="10" cols="40"></textarea>
    <br>
	<input type="button" name="Random" value="Random" onclick="textSplitter()">  
	<br>
	<textarea disabled id="results" rows="10" cols="40"></textarea>
    <br>

	<button>Finish</button>

</body>

</html>
Posted
Comments
Afzaal Ahmad Zeeshan 8-Apr-15 23:12pm    
What is the problem?
Member 11592151 9-Apr-15 1:14am    
i want to randomly distribute the names in the text area to a group. for example if i selected 3 groups and there are 10 names. it will be automatically distributed. like Group 1: 1,2,3,4 Group 2: 5,6,7 Group 3: 8,9,10. but i can't seem to get it work :(
Peter Leow 9-Apr-15 1:17am    
Where to put the split names?
Member 11592151 9-Apr-15 1:23am    
the names will be assigned into the created group
Peter Leow 9-Apr-15 1:30am    
You have not shown any JavaScript code?

1 solution

Let's see. This is a method of getting a random value in the interval between min and max:
JavaScript
function randomInt(min, max) { return Math.floor(Math.random() * (max - min + 1)) + min; }

The numbers will have statistically uniform distribution withing the said interval.

Now, if you have a set of some objects, representing, say, a group, you can put them on an array var objects = [object1, object2, ...]; it does not matter what they are. You can choose one of these objects using the array element syntax, [someIndex]. If your generate a random value for someIndex, you will get an object randomly chosen from this set. Using this idea, you can solve your problem.

—SA
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900