0

Multiple if statetments changing a variable

I have an issue, its probably just me being dumb, I need to set a value, eg x, to a certain amount, eg 300, but I also need to change the value of x depending on if another variable is at a certain number. So something like if (c === b){ x = 200;} if (c === d){ x = 500} if (c ===, etc.The issue that I'm having is that only the first assigned (x = 200)value is applied and none of the others. Any way of doing this better/less dumb would be appreciated.

15th Feb 2017, 12:23 AM
pentapen
8 odpowiedzi
+ 4
I don't know js, but what you are doing should work. A better way to do it would be to use a switch. If you are just using the value of c, then use that as your switch and all the cases as your numbers.
15th Feb 2017, 3:04 AM
J.G.
J.G. - avatar
0
Without seeing your code hard see what the problem really is, but if you're using multiple if statements, from what it looks like should be if else statements or better yet a switch. switch(c) { case b: x = 200; break; case d: x = 500; break; default: x = something; } switch is type strict when doing comparisons "===" fyi.
15th Feb 2017, 3:03 AM
ChaoticDawg
ChaoticDawg - avatar
0
Hi, sorry for the delay. Switches have helped a great deal and things seem to be working to that end. I am now noticing another issue however, which is returning if a value is not detected. For example, if (floor !== value_1)return; but applied to multiple values (floor not equaling value_1 or 2 or 3 or 4, etc), but still running if one of the values is mentioned (if the value is 3 dont return). Again, sorry for asking what is most likely a dumb question.
15th Feb 2017, 9:20 AM
pentapen
0
Can you post your code or a link to it in the playground? It's a bit difficult to make heads or tails out of what you are asking.
15th Feb 2017, 9:26 AM
ChaoticDawg
ChaoticDawg - avatar
0
Alright, added the snippet in question to the playground. Probably wont make any sense as ive had to remove a lot of the context before and after the things and changed names of things, but I commented the part I'm trying to fix. Any help or comments are appreciated.
15th Feb 2017, 9:50 AM
pentapen
0
You're using OR || so if any of those conditions return true it will return. You're also using the switch statement incorrectly. Looks like you should be switching on the group var and then your cases should be sk1 etc. See my example in my previous answer. like this: switch(group){ case SK1: length = 10; break; case SK2: length = 13; break; case SK3: length = 12; break; case SK4: length = 14; break; default: length = -1; }
15th Feb 2017, 9:56 AM
ChaoticDawg
ChaoticDawg - avatar
0
How would I make it so that it returns if none of the values are present? So if sk3 is detected it doesnt return, but if there are no defined values (group doesnt equal any of them), then it returns?
15th Feb 2017, 10:13 AM
pentapen
0
if (group !== SK1 && group !== SK2 && group !== SK3 && group !== SK4) {return;} This says that they all have to be true in order to return. When you use OR || if any of them are true it would return. Here if any of the conditions are false, the return statement is skipped.
15th Feb 2017, 6:31 PM
ChaoticDawg
ChaoticDawg - avatar