4 Replies - 2919 Views - Last Post: 23 June 2011 - 05:45 AM

#1 Wijhoops  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 21-May 11

Array problem

Posted 08 June 2011 - 11:16 AM

Hi,

as a means of learning Javascript I'm writing a script for a Deal Or No Deal game. I've started by assigning 10 different values to 10 boxes. The problem I have is making the box value unique, i.e. if box 1 is assigned the value of '1', no other boxes are given that value. If anyone can help I'd be most grateful.

 
<html>
<title>Array Test 1</title>
<head>
<script type="text/javascript">
function setBoxAmount(){
var box = ['box 1', 'box 2', 'box 3', 'box 4', 'box 5', 'box 6', 'box 7', 'box  8', 'box 9', 'box 10'];
var amount = [1,2,3,4,5,6,7,8,9,10];
var i=0;
for (i=0;i<=9;i++){
var j=Math.floor(Math.random()*10)
document.write (box[i] + ' = ' + amount[j] + '<br />');
} 
}
</script>
</head>
<body>
<script type="text/javascript">
setBoxAmount()
</script>
</body>
</html>



Is This A Good Question/Topic? 0
  • +

Replies To: Array problem

#2 ahmad_511  Icon User is offline

  • MSX
  • member icon

Reputation: 131
  • View blog
  • Posts: 722
  • Joined: 28-April 07

Re: Array problem

Posted 08 June 2011 - 01:41 PM

Hi there,
instead of checking if the random element is already used or not why not looking at it from a different angle?.

the idea is to shuffle the amount array before we use it

so, I added a simple function that swaps the passed array's elements and call it before we loop the boxes
we then use the loop counter (i) to display the amount values since no need to use a random indexes anymore
function setBoxAmount(){
var box = ['box 1', 'box 2', 'box 3', 'box 4', 'box 5', 'box 6', 'box 7', 'box  8', 'box 9', 'box 10'];
var amount = [1,2,3,4,5,6,7,8,9,10];

var i=0;
shuffle(amount)
for (i=0;i<=9;i++){
document.write (box[i] + ' = ' + amount[i] + '<br />');
} 
}

function shuffle(arr){
	for (i=0;i<arr.length;i++){
		var rnd=Math.floor(Math.random()*arr.length);
		var tmp=arr[i];
		arr[i]=arr[rnd];
		arr[rnd]=tmp;
	}
}



note:
you may ask how does the shuffle function work in spite of it doesn't return any value.
the answer is: in javascript Objects are passed by reference which means any changes to the arr argument will update the reference object amount

Hope it helps
Was This Post Helpful? 1
  • +
  • -

#3 Wijhoops  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 21-May 11

Re: Array problem

Posted 20 June 2011 - 07:50 AM

Thanks Ahmad, this is most helpful. This is a much better way than using the sort function with a random number (not truly random).
Was This Post Helpful? 0
  • +
  • -

#4 Wijhoops  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 21-May 11

Re: Array problem

Posted 23 June 2011 - 05:08 AM

Hi,

the problem I have now is that I have 22 boxes, each opened with a click that calls an openBox() function. If I put the shuffle() function INSIDE the openBox() function, it shuffles every time I open a box. If I put the shuffle() function OUTSIDE the openBox() function, the openBox() function doesn't know the shuffled variables. How can I make the shuffle() function only shuffle just the once?

Thanks
Was This Post Helpful? 0
  • +
  • -

#5 Dormilich  Icon User is online

  • 痛覚残留
  • member icon

Reputation: 3393
  • View blog
  • Posts: 9,590
  • Joined: 08-June 10

Re: Array problem

Posted 23 June 2011 - 05:45 AM

create and suffle the array outside of openBox() and the pass it a parameter. another way is creating an object that
- holds the array
- a shuffle method for the array
- and the openBox() method

e.g.
var box = {
	content: [1, 2, 3, 4, 5, 6, 7, 8, 9],
	shuffle: function() {
		function random() {
			return (Math.round(Math.random()) - 0.5);
		}
		box.content.sort(random);
		box.content.sort(random);
		box.content.sort(random);
	},
	open: function() {
		// whatever code comes here
	}
};

This post has been edited by Dormilich: 23 June 2011 - 05:54 AM

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1