Jump to content

Function Variables


shadowayex
 Share

Recommended Posts

Ok, I have a function that defines a variable from a prompt box used for specific purposes. The variable is cleared as soon as the function ends. I was wondering is there was a way to save this variable outside of the function to use it in other functions? Like a very basic code would be:

function var(){var x=prompt("Enter number","Number");}

Would there be a way to save variable x to use in another function, such as:

function write(){document.write(x);}

So if x was set to 6 in the first function, when the second function was called it would display 6. Is there a way?

Link to comment
Share on other sites

So, that will make is so the number inputed into x in one function can be called in another function. Because what it is is that I have two functions for a profit calculator, one to calculate and the other to add rows to calculate multiple items at once. They both use loops. The row adding one uses a prompt box for the user to select the amount of rows to be added, then stores it into a variable, then uses that variable to loop the row adding script that many times. What I want to happen is I want to use that same variable to loop the calculating/outputting code the same number of times as the row adding variable says. So if 5 rows were made, I want it to loop the calculating function 5 times. if 6 were added, I want it to repeat 6 times, and so on. I have all the code written, it's just the variable in the row adding function is cleared when the rows are added and the function is done.

Link to comment
Share on other sites

It's not the best code, but something I just played with to give you an idea on how to do it.

<script type="text/javascript">  var x;  function enter_number(val) {	if (val=="Enter Number") {	  x=false;	}	else {	  x="\n";	}	if (!x) {	  x=prompt("Enter Number");	}	return x;  }  function get_number() {	alert(enter_number());  }</script><input type="button" value="Enter Number" onclick="enter_number(this.value)"><input type="button" value="Get Number" onclick="get_number()">

Link to comment
Share on other sites

Ok I see where you're getting that, but I don't need the variable to show in an alert box. Here's my code:Add Items:

function additem(){ var numofitem=prompt("How many items would you like to add?\(Maximum of 50\)\nWarning: Only do this once!","Number Of Items"); var numofitems=parseInt(numofitem)+1; if (numofitem>0 && numofitem<50) {  var i=2;  for (i=2;i<=numofitems;i++)  {   document.getElementById('rows').innerHTML += "Item Name: ";   document.getElementById('rows').innerHTML += "<input type=\"text\" id=\"item" + i + "\" value=\"\" size=\"15\" />\ \;\ \;\ \;\ \;\ \;";   document.getElementById('rows').innerHTML += "Buying Price: ";   document.getElementById('rows').innerHTML += "<input type=\"text\" id=\"buy" + i + "\" value=\"\" size=\"15\" />\ \;\ \;\ \;\ \;\ \;";   document.getElementById('rows').innerHTML += "Selling Price: ";    document.getElementById('rows').innerHTML += "<input type=\"text\" id=\"sell" + i + "\" value=\"\" size=\"15\" />\ \;\ \;\ \;\ \;\ \;";   document.getElementById('rows').innerHTML += "Amount Traded: ";   document.getElementById('rows').innerHTML += "<input type=\"text\" id=\"amnt" + i + "\" value=\"\" size=\"15\" /><br />";  } }}

Note:Yes there is a specific purpose for adding one to the number, if you read carefully.Calculating Profit

function calculate(){  var finalprofit=""; var n=1; for(n=1;n<=50;n++) {  var item=document.getElementById("item" + n).value;  var buy=document.getElementById("buy" + n).value;  var sell=document.getElementById("sell" + n).value;  var amnt=document.getElementById("amnt" + n).value;  if(item=="" || buy=="" || sell=="" || amnt=="")  {   break;  }  else  {   var totalbuy=buy*amnt;   var totalsell=sell*amnt;   var profit=totalsell-totalbuy;   finalprofit += "The profit for your " + item + "s is " + profit + ".\n";  } }alert(finalprofit);}

I want the variable numofitems (set by var numofitems=parseInt(numofitem)+1;) to be able to b used in the first line of my for statement in the profit calculator part. The number is set at 50 right now, which makes it so 50 rows have to be put in order to calculate, and I don't want that. I want to change it from for(n=1;n<=50;n++) to for(n=1;n<=numofitems;n++) and actually have the value that was set in the numofitems variable by the add rows() function. See what I'm saying? Will your suggestions actually work?

Link to comment
Share on other sites

I see a probelm in your Calculating Profit script... it is saying:

var item=document.getElementById("item" + n).value;

but at the end of the loop, item wil be document.getElementById("item50").value's... change the = to += ... if you want.(just thought I would clear that up...)as for how to make the var global, I am not sure... on one of mine, I have it put the var in a hidden text box, and then recall it for the other script. is not the best way, but it works!just add this to the bottom of Add Items:

document.getElementById('hidden').value = numofitems;

and add this to Calculating Profit

 var n=1; var numofitems = document.getElementById('hidden').value; for(n=1;n<=50;n++) {

EDIT: OH! I found out what to do:say this at the top of your code(outside of all your functions):

var numofitem='';

and change 'var numofitem=prompt...;' to 'numofitem=prompt...;'forget about all of the stuff I said up there.

Link to comment
Share on other sites

Well, for a test that doesn't include a lot of code it works. But, doing it with my calculator it doesn't. I'm not sure exactly why but I think using it for the loop has something to do with it. I'm gonna do some minor tweaking and hopefully get it to work. If you have any other info/suggestions I would love to hear them.EDIT:I got it to work. Some of my experimental functions were interfering. But it's all working and now it's time to work on some more experiments and come up with more questions. Thanks a lot CloneTrooper for your awesome answers and everyone else for trying =D.

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