Expand my Community achievements bar.

Help! Referencing a "variable variable" using Javascript.

Avatar

Level 2

Hi folks,

I am have trouble referencing a "variable variable" using Javascript.  Details below...


Under my hierarchy for "form1" I have a block of variables called sco.

It contains the following variables and functions...


//////////////////////////////////////////
// BEGIN: BUILD COMMERCIAL PRODUCT LIST //
//////////////////////////////////////////
var commercialProductList=new Array(
new Array("Sydney Cityscope - Online","Sydney_Cityscope_Online"),
new Array("Sydney Cityscope - Hard Copy","Sydney_Cityscope_HardCopy"),
new Array("Sydney Cityscope - Hard Copy (w Online)","Sydney_Cityscope_HardcopyOnline"),
new Array("Sydney Lease Expiry Diary","Sydney_LeaseExpiryDiary"),

new Array("North Sydney Cityscope - Online","NorthSydney_Cityscope_Online"),
new Array("North Sydney Cityscope - Hard Copy","NorthSydney_Cityscope_HardCopy"),
new Array("North Sydney Cityscope - Hard Copy (w Online)","NorthSydney_Cityscope_HardcopyOnline"),
new Array("North Sydney Lease Expiry Diary","NorthSydney_LeaseExpiryDiary"),
);
////////////////////////////////////////
// END: BUILD COMMERCIAL PRODUCT LIST //
////////////////////////////////////////

//////////////////////////////
// BEGIN: SET GLOBAL PRICES //
//////////////////////////////
var Cityscope_Sydney_CostUser1 = 60.39;
var Cityscope_Sydney_CostUsers2to5 = 3.02;
var Cityscope_Sydney_CostUsers6to20 = 1.21;
var Cityscope_Sydney_CostHardCopyWithOnline = 14.59;
var Cityscope_Sydney_CostHardCopyWithoutOnline = 54.67;
var Cityscope_Sydney_CostHardCopyAdditional = 14.59;

var Cityscope_NorthSydney_CostUser1 = 48.40;
var Cityscope_NorthSydney_CostUsers2to5 = 2.42;
var Cityscope_NorthSydney_CostUsers6to20 = 0.97;
var Cityscope_NorthSydney_CostHardCopyWithOnline = 14.59;
var Cityscope_NorthSydney_CostHardCopyWithoutOnline = 43.54;
var Cityscope_NorthSydney_CostHardCopyAdditional = 14.59;
////////////////////////////
// END: SET GLOBAL PRICES //
////////////////////////////

//FUNCTION to get the left-hand side of s atring (like VB Left).
function Left(str, n){
if (n <= 0)
     return "";
else if (n > String(str).length)
     return str;
else
     return String(str).substring(0,n);
}


//FUNCTION to extract the Scope area name from the Product Selected.  This Area name will then be used to determine the area pricing to apply.
function GetScopeArea(ProductSelected)
{
   var ScopeAreaSelected;
   switch(true)
   {
      case (ProductSelected.indexOf("Cityscope_Online") != -1):
         ScopeAreaSelected = Left(ProductSelected, (ProductSelected.length - 17));
         break;
      case (ProductSelected.indexOf("Cityscope_HardCopy") != -1):
         ScopeAreaSelected = Left(ProductSelected, (ProductSelected.length - 19));
         break;
      case (ProductSelected.indexOf("Cityscope_HardcopyOnline") != -1):
        ScopeAreaSelected = Left(ProductSelected, (ProductSelected.length - 25));
      break;
      case (ProductSelected.indexOf("LeaseExpiryDiary") != -1):
         ScopeAreaSelected = Left(ProductSelected, (ProductSelected.length - 17));
         break;
   }
   return ScopeAreaSelected;
}

Now, in my actual form, I have a dropdown box called Item2Product which is populated on "initialize" from the sco vars as follows:
//commercialProductList
Item2Product.clearItems();

for (var i = 0; i < sco.commercialProductList.length; i++)
{
Item2Product.addItem(sco.commercialProductList[i][0],sco.commercialProductList[i][1]);
}

I also have a textfield where the user can enter the "quantity" of users for that product.  This field is called Item2Users.

I then have a field called Item2Cost that needs to be able to calculate the total cost of the product selected.

I am using a "variable variable" to reference the price, based on the product selected.  This variable variable is built, using the GetScopeArea function shown above.  The part that handles a "Cityscope Online" selection appears as follows:


//Store form values in user-friendly names.
var ProductSelected = Item2Product.rawValue;
var NumberOfUsersSelected = Item2Users.rawValue;
Item2Cost.rawValue = ProductSelected;

//Declare other vars.
var Users1Calculation;
var Users2to5Calculation;
var Users6to20Calculation;

//Get costs for product selected
var ScopeAreaSelected = sco.GetScopeArea(ProductSelected);
var CostUser1 = sco.window["Cityscope_" + ScopeAreaSelected + "_CostUser1"];
var CostUsers2to5 = sco.window["Cityscope_" + ScopeAreaSelected + "_CostUsers2to5"];
var CostUsers6to20 = sco.window["Cityscope_" + ScopeAreaSelected + "_CostUsers6to20"];

switch(true)
      {
         case (NumberOfUsersSelected < 2):
           Users1Calculation = NumberOfUsersSelected * CostUser1;
           Users2to5Calculation = 0.00;
           Users6to20Calculation = 0.00;
           break;
         case (NumberOfUsersSelected > 1 && NumberOfUsersSelected < 6):
           Users1Calculation = CostUser1;
           Users2to5Calculation = (NumberOfUsersSelected - 1) * CostUsers2to5;
           Users6to20Calculation = 0.00;
           break;
         case (NumberOfUsersSelected > 5):
           Users1Calculation = CostUser1;
           Users2to5Calculation = 4 * CostUsers2to5;
           Users6to20Calculation = (NumberOfUsersSelected - 5) * CostUsers6to20;
           break;
         default:
           alert("BROKEN: Calculate individual User Cost components based on number of users selected for the area.");
      }

//apply total cost for this item
Item2Cost.rawValue = Users1Calculation + Users2to5Calculation + Users6to20Calculation;

I am fairly certain that the crux of the problem lies with the "Get costs for product selected" vars (in red above).

I know that you can build variable variables in javascript with window[part1 + part2 + part3etc].  But I think it's my combo if this and addingh the sco reference prefix.

Does anybody know how I could do this?


Thanks very much!

Cheers,

Stanbridge

2 Replies

Avatar

Former Community Member

Hi Stanbridge,

I gave your post a few minutes before I gave up (you're basically posting an elephant asking what's wrong with it).

Spend some time isolating the real issue and I might be able to help.

You might also want to consider optimizing your code. If it's hard to read, it will be even harder to maintain (bug prone). You could probably use multi-dimensional arrays instead of string-splitting. If you have to type "switch(true)", there's probably a better way of coding it. How many lines do you have to change if you add a product, remove a product, change a price or change a description? If the answer to all these questions is 1, you've probably reached an adequate level.

I'm also trying to understand what you mean by "variable variable". A two-dimensional array? Variable index into an array? Concatenated strings forming an index into an array?

Best regards.

Avatar

Level 2

Hi there,

Thanks for the reply, and yes you're right it IS a bit of an elephant.  However, when I posted it, I had formatted it so that one little bit was highlighted showing the problematic portion of code.  The rest was just there for background (and was formatted to be much friendlier to read).

For some reason this forum wipes all text formatting.  Anyway, here's the query - now extracted from the elephant...

Let's say that stored in my form1.SCO variables object there is the following...

var Cityscope_NorthSydney_CostUser1 = 120.55;

Now let's also say that in the scope of a specific form object...

var ScopeAreaSelected = "NorthSydney";

Within the form object scope (same scope as ScopeAreaSelected), I am trying to create a variable called "CostUser1" that stores the price of the variable in the form1.SCO as follows...

var CostUser1 = sco.window["Cityscope_" + ScopeAreaSelected + "_CostUser1"];

I've used both "window" in Javascript before to make such frankenstein variable names work (using "window" to reference the top-level variable scope) but the code I am using here doesn't work.

While it's probably not the greatest all-round solution, do you know how I would get this to work?

Cheers,

Stanbridge