/    Sign up×
Community /Pin to ProfileBookmark

Logic problem (I think)

OK, this may sound a little convoluted; so I hope you all can stil understand my question.

I’m trying to construct a customizable drop-down menu. I have several global variables defined at the top of the script:

var strMenus = “Menu One=#,Menu Two=#,Menu Three=#,Menu Four=#,Menu Five=#,Menu Six=#”

var menu1= “Menu Item One=#,Menu Item Two=#,Menu Item Three=#,Menu Item Four=#,Menu Item Five=#,Menu Item Six=#”
var menu2= “Menu Item One=#,Menu Item Two=#,Menu Item Three=#,Menu Item Four=#,Menu Item Five=#,Menu Item Six=#”

etc …

I can split the first line into an array.(As there is only 1 menu header ever and the variable name is known – ‘strMenus’) Then I’m using the array data to contstruct the menu header. No problem.

But when I get down into the for loop that is constructing the menu and subsequent drop-down DIVs, I am dealing with an unknown number of potential drop-downs. I can get it — as the length of the strMenus array, but how can I ‘dynamically’ refer to a global variable? (or local)

I can pass the number of the menu, and of course they’re all prefixed with ‘menu’ — but I do not understand how I can refer to the variable ‘menu1’ and then ‘menu2’ then ‘menu3’ – etc.

Does this question make sense? Any help would be greatly appreciated.

Ben Franklin

to post a comment
JavaScript

5 Comments(s)

Copy linkTweet thisAlerts:
@karayanSep 04.2003 — I'm not sure I fully understand your problem. Are you saying that you need to work with objects whose name may change with the program logic? For example, working with lengths of arrays whose name depends on the logic? If yes, the eval() command is invaluable for this. Form the Javascript command you want as a string, using program logic, and then eval() the string.
Copy linkTweet thisAlerts:
@Ben_FranklinauthorSep 04.2003 — George,

Thanks for your response. To tell the truth I'm not sure what I mean, but I'll try to elaborate.

In simplest terms:

I need to perform a split on a predefined variable inside of a for loop. These variables are defined at the top of the page. (to allow other users to customize) the format of these variable names is always the same.(menu1, menu2, menu3, etc)

The problem is incrementally referring to these menus as the loop progresses.

tmpArray = menu(i).split(",")

Obviously this line doesn't work but I hope it illustrates my problem.

I've attached the code as a text file -- there's not much there yet, so looking at it may be easier to figure out then describing it.

Thanks again!

Kevin

[upl-file uuid=065c66ce-c3d3-49da-a8e9-7326f09e7fc4 size=2kB]menucode.txt[/upl-file]
Copy linkTweet thisAlerts:
@Ben_FranklinauthorSep 04.2003 — I've got it!

It seems I was trying to do in one step that which logically takes two steps.

tmpVarIdent = eval("menu" + (i + 1))
tmpArray = tmpVarIdent.split(",")


Thanks for the help!
Copy linkTweet thisAlerts:
@karayanSep 05.2003 — Yes, that's what I had in mind when I recommended the eval() function. Whoever first thought of this function was a genius!?
Copy linkTweet thisAlerts:
@karayanSep 05.2003 — Yes, that's what I had in mind when I recomended the eval() function. Whoever first thought of this function was a genius!?
×

Success!

Help @Ben_Franklin spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.16,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...