In the previous lesson regarding JavaScript If statements, we have a tendency to learned that we will use AN If Else If statement to check for multiple conditions, then output a special result for every condition.
For example, if the variable myColor was up to Blue, we have a tendency to might output one message. If it's Red we have a tendency to might output another, etc
Another way of doing this can be to use the JavaScript Switch statement. a plus of exploitation the switch statement is that it uses less code, that is best if you have got plenty of conditions that you just got to check for.
Exlanation of code:
When the user clicks any of the radio buttons, the onclick event handler calls the analyzeColor() perform. after we decision that perform, we have a tendency to pass within the worth of the radio button (using this.value). The perform then takes that worth and performs a switch statement on that.
The switch statement's initial line is switch (myColor). this implies that it'll perform its tests against the worth of the myColor variable.
This line is followed by a collection of "cases" inside crisp braces. it is vital to use "break" when every case - this prevents the code from running into successive case. within the case of the colour being Blue, it displays AN alert box with a message bespoken thereto color. a similar for Red. The default condition is just dead if the opposite 2 are not true (i.e. the chosen color is neither Blue nor Red).
No comments:
Post a Comment