3

I'm a JavaScript total beginner and I've gotten stuck on something that seems like it should work while trying to set a variable with a an if/else condition inside and anonymous function. Here is my code:

function changeValue(){
  var newValue = (function(){
    var getRadioValue = $( "input:radio[name=Radio1]:checked" ).val();
      if (getRadioValue === 5){
         var final = 8;
      }
      else if (getRadioValue === 4){
         var final = 5;
      }
      else if (getRadioValue === 3){
         var final = 3;
      }
      else if (getRadioValue === 2){
         var final = 1;
      }
      else if (getRadioValue === 1){
         var final = -1;
      }
      else 
      {
         var final = 0;
      }
      return final;
    })();

  alert(newValue);
}

Right now, the alert is showing 0 because none of the "ifs" are returning true, but if I set the getRadioValue variable hard coded like:

var getRadioValue = 5;

then the if/else conditional works and the alert message shows 8. That makes it seem like the .val() method isn't working. However, if I simply set the alert to:

alert(getRadioValue);

The alert message does in fact display the correctly selected radio value from the radio button set on my page. So I know that is working... Now I can't figure out where I'm going wrong. It seems like since the radio value is getting assigned to getRadioValue correctly, and the conditional is working, the whole thing should work. I'm sure it's such a basic problem, but after much research I still can't seem to get it. Any help is greatly appreciated!

3
  • any reason why a switch statement wouldn't work? Commented Dec 6, 2015 at 0:36
  • I would reccomend taking a look at stackoverflow.com/questions/359494/… , as some answers have noted, === is your problem. Commented Dec 6, 2015 at 0:37
  • Have you tested your if statements with == value equal operator ? You are using type and value comparator operator. Commented Dec 6, 2015 at 0:40

2 Answers 2

3

.val() is returning a string but you are testing for number if (getRadioValue === 5){.

You might want to do getRadioValue = parseInt(getRadioValue) first.

Demo:

getRadioValue = parseInt("5")

var final = 0; // use var once, its cleaner ;)
if (getRadioValue === 5) {
  final = 8;
} else if (getRadioValue === 4) {
  final = 5;
} else if (getRadioValue === 3) {
  final = 3;
} else if (getRadioValue === 2) {
  final = 1;
} else if (getRadioValue === 1) {
  final = -1;
}

alert(final);

Sign up to request clarification or add additional context in comments.

4 Comments

Yes, it worked. Thank you! I will post my final code below.
Nice! To keep your code clean, you might want to put var final = 0; infront, as you can see in the updated demo
He could also use the less strict == equality check.
Yes I forgot to mention that. But for user input he should get used to parseInt
0

Thanks to CodeiSir! That was the issue. Here is my final working code for others who might run into a similar problem:

function changeValue(){
 var newValue = (function(){
   var getRadioValue = $( "input:radio[name=CalcItem1]:checked" ).val();
   var RadioValue = parseInt(getRadioValue);
      if (RadioValue === 5){
         var final = 8;
      }
      else if (RadioValue === 4){
         var final = 5;
      }
      else if (RadioValue === 3){
         var final = 3;
      }
      else if (RadioValue === 2){
         var final = 1;
      }
      else if (RadioValue === 1){
         var final = -1;
      }
      else 
      {
         var final = 0;
      }
      return final;
    })();

alert(newValue);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.