0

I was trying to do a loop. However, when I input the required value (value is 1), I return me the alert part which I put in.

var ctp
        if (NumberPasses === '1') {
            ctp = 0.93
        }
        if (NumberPasses === '2') {
            ctp = 0.90
        }
        if (NumberPasses === '3') {
            ctp = 0.85
        }
        else return (
            alert("Number of tube pass: Only 1, 2 or 3!")
        )

So whenever I key in the value '1' or '2', it shows the "Number of tube pass: Only 1, 2 or 3!". However, when I key in '3', the loop accept the value and assign ctp = 0.85.

4
  • can you explain bit more Commented Apr 7, 2021 at 11:01
  • we don't know what is NumberPasses.. how are we supposed to help you? it might be potentially a int, and so the === fails for the typecheck Commented Apr 7, 2021 at 11:01
  • The NumberPasses is a user Input using the useState. If i remove the if else function, the system is able to register the input value for NumberPasses but it does go through the if else function Commented Apr 7, 2021 at 11:09
  • 1
    did you mean if() ... else if() ... else if() ... else instead of if() ... if() ... if() ... else ? If your NumberPasses is not '3' you will always get your alert Commented Apr 7, 2021 at 11:28

2 Answers 2

1

Use else if

var ctp
        if (NumberPasses === '1') {
            ctp = 0.93
        }
        else if(NumberPasses === '2') {
            ctp = 0.90
        }
        else if(NumberPasses === '3') {
            ctp = 0.85
        }
        else return (
            alert("Number of tube pass: Only 1, 2 or 3!")
        )
Sign up to request clarification or add additional context in comments.

Comments

0

You can also use else if this way:

var ctp; 
    if (NumberPasses === '1') {
        ctp = 0.93
    }
    else if (NumberPasses === '2') {
        ctp = 0.90
    }
    else if (NumberPasses === '3') {
        ctp = 0.85
    }
    
   return alert("Number of tube pass: Only 1, 2 or 3!");

Explanation about issue in your code:

In your case when you pass '1':

  • It checks first if for NumberPasses === '1' which evalutes as true
  • ctp value is assigned 0.93
  • Then it checks for NumberPasses === '2' which evalutes as false
  • Then it checks for NumberPasses === '3' which evalutes as false
  • It goes to else case and returns alert("Number of tube pass: Only 1, 2 or 3!");

Similarly it happens for '2':

  • It checks first if for NumberPasses === '1' which evalutes as false
  • Then it checks for NumberPasses === '2' which evalutes as true
  • ctp value is assigned 0.90
  • Then it checks for NumberPasses === '3' which evalutes as false
  • It goes to else case and returns alert("Number of tube pass: Only 1, 2 or 3!");

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.