Connecting Tech Pros Worldwide Forums | Help | Site Map

Simple question about functions/strings

Member
 
Join Date: Feb 2008
Posts: 123
#1: Mar 11 '08
This seems a really simple question to answer, but being fairly new to JS, I have no idea how to do it:

I'd like to insert a string which contains JS commands set by one function into another function. Below is sample code which hopefully explains what I want to do:

Expand|Select|Wrap|Line Numbers
  1. func1 () {
  2. ---blah---
  3. if (t == 1) {
  4. test = "
  5. if (j1=="right" && i1<24) {
  6. i1 = parseInt(i1)+1;
  7. } else {
  8. i1 = parseInt(j1); }
  9. ";
  10. ---blah---
  11. OR SET test USING OTHER CONDITIONS
  12. ---blah---
  13. }
  14.  
  15. funcA() {
  16. ---blah---
  17. INSERT test HERE
  18. ---blah---
  19. }
  20.  
Probably alarmingly simple, but can't find any example code.

Thanks

iam_clint's Avatar
Forum Leader
 
Join Date: Jul 2006
Location: Oklahoma
Posts: 1,076
#2: Mar 11 '08

re: Simple question about functions/strings


well you cannot make one function out of another however this may not be what your asking.. to set a global variable you put var test; outside of the functions like so..

Expand|Select|Wrap|Line Numbers
  1. <script>
  2. var test;
  3. function a() {
  4.   test="blah!";
  5. }
  6.  
  7. function b() {
  8.   alert(test);
  9. }
  10. </script>
  11.  
We can help get you a solution to fix your problem if you care to explain what you are really trying to accomplish.
rnd me's Avatar
Expert
 
Join Date: Jun 2007
Location: Urbana IL
Posts: 411
#3: Mar 11 '08

re: Simple question about functions/strings


higher-order functions are indeed possible in javascript, as are dynamic functions:

Expand|Select|Wrap|Line Numbers
  1. var funcA= new Function(test);
  2. alert(funcA.toString());
  3.  
Member
 
Join Date: Feb 2008
Posts: 123
#4: Mar 11 '08

re: Simple question about functions/strings


Thanks for the replies.

Maybe I'm going about this wrong. Let me try to explain what I want to do:

I have a function within which a number of "constants" are set depending on which value a pull down is set to. To avoid setting about 10 "constants" (based on the pull down selected), I thought I could drop in code like a string.

So....

If the drop down (t) is set to 1, include the following code at a specific point in funcA (a different function):

[code]
if (j1=="right" && i1<24) {
i1 = parseInt(i1)+1;
} else if (j1=="down" && i1<19) {
i1 = parseInt(i1)+6;
} else {
i1 = parseInt(j1); }
[code]

If the drop down (t) is set to 2, include the following code at the same specific point in funcA:

Expand|Select|Wrap|Line Numbers
  1. if (j1=="right" && i1<48) {
  2. i1 = parseInt(i)+1;
  3. } else if (j1=="down" && i1<43) {
  4. i1 = parseInt(i1)+6;
  5. } else {
  6. i1 = parseInt(j1); }
  7.  
There are actually a lot more "else if" statements, so it would get quite difficult to set "24/48", "19/43", etc, as variables --- I think!

Thanks!
Member
 
Join Date: Feb 2008
Posts: 123
#5: Mar 11 '08

re: Simple question about functions/strings


Thinking about it, those number are all related, so I guess I could pass a variable and use operators to figure them all out.

However, for future reference though, could someone please tell me if the above question is possible.
iam_clint's Avatar
Forum Leader
 
Join Date: Jul 2006
Location: Oklahoma
Posts: 1,076
#6: Mar 11 '08

re: Simple question about functions/strings


Quote:

Originally Posted by phub11

Thanks for the replies.

Maybe I'm going about this wrong. Let me try to explain what I want to do:

I have a function within which a number of "constants" are set depending on which value a pull down is set to. To avoid setting about 10 "constants" (based on the pull down selected), I thought I could drop in code like a string.

So....

If the drop down (t) is set to 1, include the following code at a specific point in funcA (a different function):

Expand|Select|Wrap|Line Numbers
  1. if (j1=="right" && i1<24) {
  2. i1 = parseInt(i1)+1;
  3. } else if (j1=="down" && i1<19) {
  4. i1 = parseInt(i1)+6;
  5. } else {
  6. i1 = parseInt(j1); }
  7.  
If the drop down (t) is set to 2, include the following code at the same specific point in funcA:

Expand|Select|Wrap|Line Numbers
  1. if (j1=="right" && i1<48) {
  2. i1 = parseInt(i)+1;
  3. } else if (j1=="down" && i1<43) {
  4. i1 = parseInt(i1)+6;
  5. } else {
  6. i1 = parseInt(j1); }
  7.  
There are actually a lot more "else if" statements, so it would get quite difficult to set "24/48", "19/43", etc, as variables --- I think!

Thanks!


You could always put your numbers into an array and then loop through the array with a for loop. Example
Expand|Select|Wrap|Line Numbers
  1. <script>
  2. var n1 = [48,43,22,12,23,13];
  3. var i1 = 48;
  4. var j1 = "down";
  5. for (i=0; i<n1.length; i++) 
  6. {
  7.  if (j1=="down" && i1==n1[i])
  8.  {
  9.  alert(n1[i]);
  10.   //do stuff
  11.  }
  12. }
  13. </script>
  14.  
rnd me's Avatar
Expert
 
Join Date: Jun 2007
Location: Urbana IL
Posts: 411
#7: Mar 12 '08

re: Simple question about functions/strings


why don't you simply make a couple extra functions with the code needed for each drop down position. in your function, you can then call that wrapper function in the middle of another function. this forces the script to evaluate the code later, after the surrounding variables have been set.

example
Expand|Select|Wrap|Line Numbers
  1. function ver1(a){ return a + 10 }
  2. function ver2(a){ return a + 50 }
  3.  
  4. function testMe(arg){
  5.   var a = parseInt(arg);
  6.   if(a>10) return ver1(a)
  7.   if(a<10) return ver2(a)
  8. }
Reply