Not sure what I am doing wrong, can't seem to get the right syntax.
I have a first IF loop when that works, then it drops into a second and third IF loop.
But when the first IF is not valid, it drops into an ELSE condition and continues processing from there.
a = 200;
var status = "status";
if (a== 100){
b = 200;
if (b = 200);
c=300;
if (c = 300);
status = "first";
}else if (a != 100) {
b = 200;
if (b = 200);
c=300;
if (c = 300);
status = "second";
}
return msg;
As @knolleary points out, you're using = in some if statements, which is assignment, when you should be using == or ===.
Some of your if statements end in a ;, which ends them. The statement on the next line is therefore not part of the if, and will always execute.
You have indented some of the code as if it should be part of a block. Python works this way; Javascript does not. For Javascript you need to define a block using{...} as you have done for the if (a == 100) {... The test for b and c are therefore within the test for a, but c is not within b.
The else if can just be simplified to else given if a is 100, the else condition is a is not 100.
You also said
"But when the first IF is not valid, it drops into an ELSE condition and continues processing from there"
This is what is supposed to happen. Everything within the {...} after the first if is treated as one block. If the if fails, the else (if it exists) is executed.
Your code should look more like that below. Assuming that's what you want to achieve, because I'm guessing it's just made up for the purpose of posting as it does nothing useful.
if (a === 100) {
b = 200;
if (b === 200) {
c = 300;
if (c === 300)
status = "first";
}
} else {
b = 200;
if (b === 200) {
c = 300;
if (c === 300)
status = "second";
}
}
My recommendation, particularly for beginners, is always to use {..} for if statements and blocks, even for single line blocks. It makes the code much less prone to the sort of errors you have in your code.