How to shorten/combine "if()" statements?
-
Is there a way to shorten or combine if() statements? Currently I have if age<21 there is an alert, then else if age>21 there is an alert, another else if age==21 alert, and an else alert. Is there a way to combine age<21, age>21, age==21 into one statement with their individual alerts? Thanks, I am taking my first Javascript class right now.
-
Is there a way to shorten or combine if() statements? Currently I have if age<21 there is an alert, then else if age>21 there is an alert, another else if age==21 alert, and an else alert. Is there a way to combine age<21, age>21, age==21 into one statement with their individual alerts? Thanks, I am taking my first Javascript class right now.
use case/switch statement
switch (true) {
case (age == 21):
alert(" equal");
break;
case (age> 21):
alert(" older");
break;
case (age < 21):
alert("younger");
break;} -
Is there a way to shorten or combine if() statements? Currently I have if age<21 there is an alert, then else if age>21 there is an alert, another else if age==21 alert, and an else alert. Is there a way to combine age<21, age>21, age==21 into one statement with their individual alerts? Thanks, I am taking my first Javascript class right now.
<script>
function myfun() {
var age = 21; //change value of age and check it.
(age < 21) ? alert('less than 21') : (age > 21) ? alert('greater than 21') : (age == 21) ? alert('equl 21') : alert('else part');
}
</script>It's give you result 100% with single line. :)
-
<script>
function myfun() {
var age = 21; //change value of age and check it.
(age < 21) ? alert('less than 21') : (age > 21) ? alert('greater than 21') : (age == 21) ? alert('equl 21') : alert('else part');
}
</script>It's give you result 100% with single line. :)
-
The third expression
"age == 21"
is redundant. If it is not less than and not greater than then there is only one other value it can have.Hello Richard MacCutchan, I think you have not tested well in my case it's worked fine. i think you should have to check again. if any doubt then reply me. just change value of age like 19,21,22 and 'blah..' it gives you all kind of alert..please try it out.
-
Hello Richard MacCutchan, I think you have not tested well in my case it's worked fine. i think you should have to check again. if any doubt then reply me. just change value of age like 19,21,22 and 'blah..' it gives you all kind of alert..please try it out.
Godhaniketan wrote:
Hello Richard MacCutchan, I think you have not tested well in my case it's worked fine. i think you should have to check again. if any doubt then reply me. just change value of age like 19,21,22 and 'blah..' it gives you all kind of alert..please try it out.
Well I guess you do not understand that simple statement only requires two if parts. If age is less than 21 : Alert1 If age is greater than 21 : Alert2 Else: age must be equal to 21, so the test for equality is not needed.
-
Godhaniketan wrote:
Hello Richard MacCutchan, I think you have not tested well in my case it's worked fine. i think you should have to check again. if any doubt then reply me. just change value of age like 19,21,22 and 'blah..' it gives you all kind of alert..please try it out.
Well I guess you do not understand that simple statement only requires two if parts. If age is less than 21 : Alert1 If age is greater than 21 : Alert2 Else: age must be equal to 21, so the test for equality is not needed.
Hello Richard MacCutchan, have you read question of "Member 11849688" he written clearly that "else if age==21 alert" that means he/she wants alert on equality condition also. please read out the question properly. he/she want 4 alert <, >, ==, else part.
-
Hello Richard MacCutchan, have you read question of "Member 11849688" he written clearly that "else if age==21 alert" that means he/she wants alert on equality condition also. please read out the question properly. he/she want 4 alert <, >, ==, else part.
Godhaniketan wrote:
Hello Richard MacCutchan, have you read question of "Member 11849688" he written clearly that "else if age==21 alert" that means he/she wants alert on equality condition also. please read out the question properly. he/she want 4 alert <, >, ==, else part.
Yes, and that is what my logic provided. If you really cannot understand simple boolean expressions then ...
-
Godhaniketan wrote:
Hello Richard MacCutchan, have you read question of "Member 11849688" he written clearly that "else if age==21 alert" that means he/she wants alert on equality condition also. please read out the question properly. he/she want 4 alert <, >, ==, else part.
Yes, and that is what my logic provided. If you really cannot understand simple boolean expressions then ...
Yes i understand your logic.. i will remove redundancy. thanks
-
Yes i understand your logic.. i will remove redundancy. thanks