o In our discussion of conditionals, we saw how to check the veracity of a single condition via a comparator:
if ( x > some_value )
o We have also seen the limitations of such an approach. Let us say we wanted to discover if x lay between two values, say val_1 and val_2. There are a number of ways we could achieve this. In our example on student grades, we learned that we could use an if…else pair to achieve this effect:
if ( x > val_1 )
if ( x > val_2 )
…do something else… }
o The above code achieves what we want – for the second branch, x must lie between val_2 and val_1 (assuming val_1 is greater than val_2, of course). However, it’s rather unwieldy, and does not scale elegantly to checking three conditions (say we wanted to check if x was an even number as well), or in fact to ten conditions.
o Enter Logical Operators. These operators are used to join together conditional checks and return true or false depending on whether all or any of the checks are true.
o In English, we refer to these operators by using the words “AND” and “OR”.
o For example, say we wanted to do something each Tuesday at 8pm. We would want to check whether the current day was Tuesday, and whether the time was 8pm.
o Another example: Let’s say we wanted to do something on the first Tuesday of each month, and also on the 3rd of the month as well. We would have to check whether the current day was the first Tuesday of the month, or whether it was the 3rd day of the month.
Note in the last example, if both conditions were true, then we would be on Tuesday the 3rd and would perform the action. In other words, an or condition allows for either one, or the other, or bothconditions to be true.
( 100 > 10 && 5 < 8 )
translates as “if 100 is greater than 10 and 5 is less than 8”. In this case, the result is true.
( 100 > 200 && 4 < 9 )
in this case, the result is false. Note here that only the first condition is actually checked. Since and requires both comparisons to be true, as soon as it finds a false one it stops checking. This can be useful.
( 100 > 10 || 9 < 8 )
translates as “if 100 is greater than 10 or 9 is less than 8”. In this case, the result is true, since at least one of the conditions is met.
( 100 > 200 || 4 > 9 )
in this case, the result is false since neither of the comparisons are true. Finally:
( 100 > 200 || 5 < 2 || 3 > 2 )
in this case, the result is true. Any one of the three being true will provide this result.
o As we can see from the last example, this method of checking scales to any number of conditions. We can also mix and match the operators. For example:
( ( 100 > 200 && 100 > 300 ) || 100 > 2 )
in this case, the and condition evaluates to false, but since either that or the last condition has to be true to return true, the overall condition returns true as 100 is indeed greater than 2.
o This sort of complex logic can take a while to comprehend, and will not form a set part of the course. However, it is useful to be aware of it.
o Ensure that you have a script element in the head area of your document, and one in the body area.
o Copy the file available_plugins.js from the network drive (your tutor will demonstrate this), and open it using NotePad’s File > Open command.
o Copy and paste the entire contents of available_plugins.js into your current project file, into the script element in the head area of your page.
o Have a read through the code. Note that it defines a large, two dimensional array. The array has a list of various components that can be present in web browsers (such as Flash or Quicktime)
o Add a function to the head area script element, called flash_exists(). This function should use a for loop to check each of the elements of the available_plugins array and establish if Flash is present.
o Add a further function to the head area script element, called quicktime_exists(). This function should also use a for loop to check each element of the array, this time returning true if Quicktime is present. o Finally, add a function to the head area script element called both_quicktime_and_flash_exist(). This function should call both of the previous functions, store their results in a variable, and produce an alert box containing the message: o “Both Quicktime and Flash” if both functions returned true; or:
o “One of Quicktime or Flash is missing” if either of the functions return false.
Call the final function from the body area script element.
o Check your results in your browser.