-3

Need some help with the following javascript problem that won't resolve. I'm a novice and doing my best to find a good solution so I can complete my thesis research. The code is designed to run inside an online survey program called Qualtrics. Each var represents pipped data from question responses. I have tested these and they work fine.

One of the switch works fine outside the if-else statement. However, when combined, things don't work.

Any help much appreciated.

var storeLow = ${q://QID83/ChoiceGroup/SelectedChoices};
var storeHigh = ${q://QID82/ChoiceGroup/SelectedChoices};
var cash = 0;
if(storeLow >= 1)
{
 switch(storeLow) {
  case 5: 
  cash = 200; 
  break;

  case 3:
  case 6:
  case 11:
  case 12:
  cash = 150;
  break;

  case 1:
  case 2:
  case 4:
  case 7:
  case 8:
  case 9:
  case 10:
  case 13:
  case 14:
  case 15:
  case 16:
  cash = 100;
  break;

  default:
  cash = 50;
  break;
 }
} else {
 switch(storeHigh) {
  case 3:
  cash = 200;
  break;

  case 6:
  case 10:
  case 11:
  case 15:
  cash = 150;
  break;

  case 1:
  case 2:
  case 4:
  case 5:
  case 7:
  case 8:
  case 9:
  case 12:
  case 13:
  case 14:
  case 16:
  cash = 100;
  break;

  default:
  cash = 50;
  break;
 }
}
655321
  • 411
  • 4
  • 26
Will
  • 7
  • 1
  • 5
  • 1
    The syntax looks reasonable. Define "doesn't work". – Alnitak Sep 29 '11 at 09:22
  • 1
    What is exactly is happening? What "isn't working"? The code seems fine to me. – Zecc Sep 29 '11 at 09:26
  • You should probably put your `case`s in numeric order, by the way — even if you end up having to repeat assignments. It will just make it easier to read, IMO. – Zecc Sep 29 '11 at 09:29
  • To give some more background to the problem, the code here is used to load a java applet and parse the starting cash condition as a parameter. I have two separate experimental treatments that could potentially come to the java applet, and thus needed a way to load the cash value associated with criteria from either storeLow or storeHigh. The reason what I say 'doesn't work' was that the java applet was not initiating due to the cash function here not returning valid data. Sorry for the confusion, and thank you to all who have contributed – Will Sep 29 '11 at 12:34

2 Answers2

0

A better approach will be using a javascript object to assign Cash ranges, instead of using ugly code with so many switch cases:

eg.

var myCashCriteria = { '15': 100 , '16' : 100, '17' : 100,'5' : 200 }
var myCash = getCash(16);


function getCash(num)
{
   var numKey = num + '';   // convert num to string
   if(myCashCriteria[numKey])
           return myCashCriteria[numKey];
   else
           return 0 ;   // default value
}
DhruvPathak
  • 42,059
  • 16
  • 116
  • 175
  • 2
    You're using strings as keys in your example. They should be integers instead. In any case I too would suggest using this approach instead, but that's more just a matter of taste. Plus, don't forget to check for the need to set a default value. – Zecc Sep 29 '11 at 09:31
  • Actually I forgot that property names (what I was calling "keys") are always a string in JS. Indexing an object with a number will convert that number to a string first. So using quotes around the keys in your object literal is actually more correct than not. :) – Zecc Sep 29 '11 at 10:13
  • Thanks a lot @DhruvPathak. I like your suggestion, and your comment about the 'ugly' code is completely justified. The reason for using the switch and the if-else statements are that they also exist inside another function. I will try to test your suggestion and report back how it goes. – Will Sep 29 '11 at 12:29
0

I'm going to take a wild guess here and believe that maybe one of your variables is actually carrying string values instead of integers. Try substituting:

switch(storeLow) {

...

switch(storeHigh) {

with

switch( parseInt(storeLow, 10) ) {

...

switch( parseInt(storeHigh, 10) ) {
Zecc
  • 4,220
  • 19
  • 17
  • Thanks so much for this. I had completely forgotten the parseInt() function that is required to convert the text field entries where the variable data is extracted from. I have tested it and got it working as per the original script with the following changes: Change: var storeLow = ${q://QID83/ChoiceGroup/SelectedChoices}; var storeHigh = ${q://QID82/ChoiceGroup/SelectedChoices};' to var storeLow = parseInt("${q://QID83/ChoiceGroup/SelectedChoices}"); var storeHigh = parseInt("${q://QID82/ChoiceGroup/SelectedChoices}"); – Will Sep 29 '11 at 12:23
  • @Will A heads-up just in case: while you should probably be fine calling `parseInt` without a second argument, if you receive numbers starting with a 0 (zero) they will be interpreted as if written in octal, yielding unexpected results. See [this other question](http://stackoverflow.com/questions/850341/workarounds-for-javascript-parseint-octal-bug) for more information. – Zecc Sep 29 '11 at 19:24
  • Thanks a lot. Yes, wrapping the variables inside parseInt() is working. I have a full test version now that calls the java applet, sets the starting cash value as per the variables parsed, runs the applet, then when done takes a value from the applet and puts this into another hidden text field entry. This all works great, including calling the output from the java applet. However, I'm now onto debugging it outside the test version. Thanks a whole other story :) – Will Sep 30 '11 at 02:51