Skip to main content
Inspiring
August 30, 2017
Answered

What have I done wrong with this IF statement?

  • August 30, 2017
  • 1 reply
  • 870 views

Hi,

I'm trying to get a certain buttons to appear depending on score returned in a  given box...but it's not working.

I can get it to work when there are fewer options and threescore is a > thing, but equal to and more options, I'm struggling with.

Can anyone see where i've gone wrong?

var nResult = this.getField("score").value;

if ( nResult = 200 ) {

this.getField("adventure_seeker").display = display.hidden;

this.getField("mountain_climber").display = display.hidden;

this.getField("deep_diver").display = display.visible;

this.getField("JOINT AS_MC").display = display.hidden;

this.getField("JOINT AS_DD").display = display.hidden;

this.getField("JOINT DD_MC").display = display.hidden;

this.getField("no_score").display = display.hidden;

}

else if ( nResult = 160 ) {

this.getField("adventure_seeker").display = display.hidden;

this.getField("mountain_climber").display = display.hidden;

this.getField("deep_diver").display = display.visible;

this.getField("JOINT AS_MC").display = display.hidden;

this.getField("JOINT AS_DD").display = display.hidden;

this.getField("JOINT DD_MC").display = display.hidden;

this.getField("no_score").display = display.hidden;

}

else if ( nResult = 151 ) {

this.getField("adventure_seeker").display = display.hidden;

this.getField("mountain_climber").display = display.hidden;

this.getField("deep_diver").display = display.visible;

this.getField("JOINT AS_MC").display = display.hidden;

this.getField("JOINT AS_DD").display = display.hidden;

this.getField("JOINT DD_MC").display = display.hidden;

this.getField("no_score").display = display.hidden;

}

else if ( nResult = 40 ) {

this.getField("adventure_seeker").display = display.visible;

this.getField("mountain_climber").display = display.hidden;

this.getField("deep_diver").display = display.hidden;

this.getField("JOINT AS_MC").display = display.hidden;

this.getField("JOINT AS_DD").display = display.hidden;

this.getField("JOINT DD_MC").display = display.hidden;

this.getField("no_score").display = display.hidden;

}

else if ( nResult = 31 ) {

this.getField("adventure_seeker").display = display.visible;

this.getField("mountain_climber").display = display.hidden;

this.getField("deep_diver").display = display.hidden;

this.getField("JOINT AS_MC").display = display.hidden;

this.getField("JOINT AS_DD").display = display.hidden;

this.getField("JOINT DD_MC").display = display.hidden;

this.getField("no_score").display = display.hidden;

}

else if ( nResult = 80 ) {

this.getField("adventure_seeker").display = display.visible;

this.getField("mountain_climber").display = display.hidden;

this.getField("deep_diver").display = display.hidden;

this.getField("JOINT AS_MC").display = display.hidden;

this.getField("JOINT AS_DD").display = display.hidden;

this.getField("JOINT DD_MC").display = display.hidden;

this.getField("no_score").display = display.hidden;

}

else if ( nResult = 71 ) {

this.getField("adventure_seeker").display = display.visible;

this.getField("mountain_climber").display = display.hidden;

this.getField("deep_diver").display = display.hidden;

this.getField("JOINT AS_MC").display = display.hidden;

this.getField("JOINT AS_DD").display = display.hidden;

this.getField("JOINT DD_MC").display = display.hidden;

this.getField("no_score").display = display.hidden;

}

else if ( nResult = 4 ) {

this.getField("adventure_seeker").display = display.hidden;

this.getField("mountain_climber").display = display.visible;

this.getField("deep_diver").display = display.hidden;

this.getField("JOINT AS_MC").display = display.hidden;

this.getField("JOINT AS_DD").display = display.hidden;

this.getField("JOINT DD_MC").display = display.hidden;

this.getField("no_score").display = display.hidden;

}

else if ( nResult = 13 ) {

this.getField("adventure_seeker").display = display.hidden;

this.getField("mountain_climber").display = display.visible;

this.getField("deep_diver").display = display.hidden;

this.getField("JOINT AS_MC").display = display.hidden;

this.getField("JOINT AS_DD").display = display.hidden;

this.getField("JOINT DD_MC").display = display.hidden;

this.getField("no_score").display = display.hidden;

}

else if ( nResult = 53 ) {

this.getField("adventure_seeker").display = display.hidden;

this.getField("mountain_climber").display = display.visible;

this.getField("deep_diver").display = display.hidden;

this.getField("JOINT AS_MC").display = display.hidden;

this.getField("JOINT AS_DD").display = display.hidden;

this.getField("JOINT DD_MC").display = display.hidden;

this.getField("no_score").display = display.hidden;

}

else if ( nResult = 62 ) {

this.getField("adventure_seeker").display = display.hidden;

this.getField("mountain_climber").display = display.visible;

this.getField("deep_diver").display = display.hidden;

this.getField("JOINT AS_MC").display = display.hidden;

this.getField("JOINT AS_DD").display = display.hidden;

this.getField("JOINT DD_MC").display = display.hidden;

this.getField("no_score").display = display.hidden;

}

else if ( nResult = 22 ) {

this.getField("adventure_seeker").display = display.hidden;

this.getField("mountain_climber").display = display.hidden;

this.getField("deep_diver").display = display.hidden;

this.getField("JOINT AS_MC").display = display.visible;

this.getField("JOINT AS_DD").display = display.hidden;

this.getField("JOINT DD_MC").display = display.hidden;

this.getField("no_score").display = display.hidden;

}

else if ( nResult = 102 ) {

this.getField("adventure_seeker").display = display.hidden;

this.getField("mountain_climber").display = display.hidden;

this.getField("deep_diver").display = display.hidden;

this.getField("JOINT AS_MC").display = display.hidden;

this.getField("JOINT AS_DD").display = display.visible;

this.getField("JOINT DD_MC").display = display.hidden;

this.getField("no_score").display = display.hidden;

}

else if ( nResult = 120 ) {

this.getField("adventure_seeker").display = display.hidden;

this.getField("mountain_climber").display = display.hidden;

this.getField("deep_diver").display = display.hidden;

this.getField("JOINT AS_MC").display = display.hidden;

this.getField("JOINT AS_DD").display = display.hidden;

this.getField("JOINT DD_MC").display = display.visible;

this.getField("no_score").display = display.hidden;

}

else {

this.getField("adventure_seeker").display = display.hidden;

this.getField("mountain_climber").display = display.hidden;

this.getField("deep_diver").display = display.hidden;

this.getField("JOINT AS_MC").display = display.hidden;

this.getField("JOINT AS_DD").display = display.hidden;

this.getField("JOINT DD_MC").display = display.hidden;

this.getField("no_score").display = display.visible;

}

Thanks in advance

This topic has been closed for replies.
Correct answer Test Screen Name

Ah, this is an easy one because we've all done this, over and over...

if ( nResult = 200 )

Means something but not at all what you think. The test for equality is not one equals sign but two.

if ( nResult == 200 )

and throughout.

1 reply

Test Screen NameCorrect answer
Legend
August 30, 2017

Ah, this is an easy one because we've all done this, over and over...

if ( nResult = 200 )

Means something but not at all what you think. The test for equality is not one equals sign but two.

if ( nResult == 200 )

and throughout.

Inspiring
August 30, 2017

wooo hoooo!!! Thanks Test Screen Name! I can breathe again

Inspiring
August 30, 2017

You could also set the display to all the fields to hidden and then only switch the needed field to visible for a given score. JavaScript also has the "switch" statement so one does not need to use the nested if statement.

this.getField("adventure_seeker").display = display.hidden;
this.getField("mountain_climber").display = display.hidden;
this.getField("deep_diver").display = display.visible;
this.getField("JOINT AS_MC").display = display.hidden;
this.getField("JOINT AS_DD").display = display.hidden;
this.getField("JOINT DD_MC").display = display.hidden;
this.getField("no_score").display = display.hidden;

switch(this.getField("score").valueAsString)
{
case "200":
this.getField("deep_diver").display = display.visible;
break;

case "160":
case "151":
this.getField("deep_diver").display = display.visible;
break
case "40":
case "31":
case "80":
case "71":
this.getField("adventure_seeker").display = display.visible;
break;
case "4":
case "13":
case "53":
case "62":
this.getField("mountain_climber").display = display.visible;
break;
case "22":
this.getField("JOINT AS_MC").display = display.visible;
case "102":
this.getField("JOINT AS_DD").display = display.visible;
break;
case "120":
this.getField("JOINT DD_MC").display = display.visible;
break;
default:
this.getField("no_score").display = display.visible;
break;
} // end switch score;

This cuts down on the number of lines to code by starting with a known state and changing the minimum of fields and uses a compound case statement to reduce the amount of redundant code. This could also be done with the "if" statement by using a complex logical statement use the logical OR operator.