Jump to content

return a value from function and put them in variables


lottape
 Share

Recommended Posts

HI!I´ve got an assignement tha involves these dices and getting a value from them, that I can return from showRandomPict and then save in a form. So that it gets possible to calculate a sum.I´ve been trying diffrent approaches, but can´t get it to work. :) I would really appreciate some help. :) This is the code:

<script type="text/javascript" language="javascript">	   		function showPict(randNumber , imgNr){		if (randNumber == 1 ) imgUrl = "dice/1.gif";		else if (randNumber == 2 ) imgUrl = "dice/2.gif";		else if (randNumber == 3 ) imgUrl = "dice/3.gif";		else if (randNumber == 4 ) imgUrl = "dice/4.gif";		else if (randNumber == 5 ) imgUrl = "dice/5.gif";		else if (randNumber == 6 ) imgUrl = "dice/6.gif";					document["largeImg" + imgNr].src = imgUrl;	}				function randomInteger(randNumber) {		var randNumber = Math.random();		var randNumber = Math.floor(6*randNumber)+1;		return randNumber;	}				function showRandomPict(imgNr) {		var pictNr = randomInteger(6);		showPict(pictNr , imgNr);	}	   </script></head><body>	<p> Roll The Dice! </p>	<div id= largePict>		<a href="#" return false;>		<img name="largeImg1" src="dice/1.gif" alt="Ett" onclick="showRandomPict(1)" />		</a>		 <a href="#" return false;>		<img name="largeImg2" src="dice/1.gif" alt="Ett" onclick="showRandomPict(2)" />		</a>		<a href="#" return false;>		<img name="largeImg3" src="dice/1.gif" alt="Ett" onclick="showRandomPict(3)" />		</a>	</div>	<div id="dice">		<p>			<a href="#" onclick =  "showRandomPict(1); showRandomPict(2); showRandomPict(3); return false">Roll all Dices</a>		</p>		<p>			<form name="result">			</form>		</p>	</div></body></html>

Link to comment
Share on other sites

well, what are you stuck with? What errors are you getting?real quickly, this

	function randomInteger(randNumber) {		var randNumber = Math.random();		var randNumber = Math.floor(6*randNumber)+1;		return randNumber;	}

will overwrite the first instance of randNumber, which you are trying to floor.I also wouldn't have this<a href="#" return false>, you can't really mix markup and scripting like that. If you already have an onclick handler on the image, why even bother with <a> tag?Instead of running the same function three times, why not just create on function directly made for rolling all the dice?There are quite a few little things here and there that are worth pointing out, but it would almost be at the cost of re-writing it. Not to say that what you have isn't a good start, but it definitely needs a little more organization.I would consider using document.getElementById to reference the image elements.the form itself seems un-needed as well.some thoughts: create an array for the dice image, and use the number you are passing (-1) in as index for which picture to show. I would also create an array to hold references to the image elements.i.e.

var basePath = "dice/";var diceImagesArray = [];for(var i  = 0; i < 6; i++){  diceImagesArray[i] = basePath + (++i) + ".gif";};

this should give you all the paths you need in an indexable structure, so you can just get an image src like so:

var index = randNumber - 1;var imageUrl = diceImagesArray[index];

no long if/else statements. Just some ideas to get you rolling I suppose.

Edited by thescientist
Link to comment
Share on other sites

You can remove returns: remove randomInteger and showPict, they are not needed and just flood code. Your showRandomPict function will be the following:

	function showRandomPict(imgNr){		document["largeImg" + imgNr].src = "dice/" + (Math.floor(6*Math.random())+1) + ".gif";	}

If your image names are not dice/number.gif, use switch.And, as thescientist said, argument names and variable names must be different.

Edited by SiPlus
Link to comment
Share on other sites

well, what are you stuck with? What errors are you getting?real quickly, this
	function randomInteger(randNumber) {		var randNumber = Math.random();		var randNumber = Math.floor(6*randNumber)+1;		return randNumber;	}

will overwrite the first instance of randNumber, which you are trying to floor.I also wouldn't have this<a href="#" return false>, you can't really mix markup and scripting like that. If you already have an onclick handler on the image, why even bother with <a> tag?Instead of running the same function three times, why not just create on function directly made for rolling all the dice?There are quite a few little things here and there that are worth pointing out, but it would almost be at the cost of re-writing it. Not to say that what you have isn't a good start, but it definitely needs a little more organization.I would consider using document.getElementById to reference the image elements.the form itself seems un-needed as well.some thoughts: create an array for the dice image, and use the number you are passing (-1) in as index for which picture to show. I would also create an array to hold references to the image elements.i.e.

var basePath = "dice/";var diceImagesArray = [];for(var i  = 0; i < 6; i++){  diceImagesArray[i] = basePath + (++i) + ".gif";};

this should give you all the paths you need in an indexable structure, so you can just get an image src like so:

var index = randNumber - 1;var imageUrl = diceImagesArray[index];

no long if/else statements. Just some ideas to get you rolling I suppose.

Yeah, I know it´s a mess...I don´t really know what I´m doing.... :) The dices have to change number randomly one-by-one aswell as all at the same time.It says in the instructions that I have to have either a span-tag or a form, thats is why I got it.I´ll check this out!Thanks!! :)
Link to comment
Share on other sites

You can remove returns: remove randomInteger and showPict, they are not needed and just flood code. Your showRandomPict function will be the following:
	function showRandomPict(imgNr){		document["largeImg" + imgNr].src = "dice/" + (Math.floor(6*Math.random())+1) + ".gif";	}

If your image names are not dice/number.gif, use switch.And, as thescientist said, argument names and variable names must be different.

THANKS! :)
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...