March 29, 2024

Answer to JavaScript Exercise Q5

 

i)

var a = 'Antelope';
var b ='Zebra';
if (a < b)
{
  document.write('Antelope is before Zebra' + '<br>');
}
else
{
  document.write('Zebra is before Antelope' + '<br>');
}

Notes:

  • There are usually several ways of implementing the required Boolean logic. Remember that the condition must evaluate to true for the if branch to be executed. Therefore the condition (a < b) can also be written as
    (!(a > b))
    Another alternative is (b > a)

ii)

var a = 'antelope';
var b ='Zebra';
if (a > b)
{
  document.write('antelope is before Zebra' + '<br>');
}
else
{
  document.write('Zebra is before antelope' + '<br>');
}

Notes:

  • The above condition can be written as either
    (b < a)
    or
    (!(a < b))

 

Back « JavaScript Boolean conditions

 

 

 

 

 

 

 

 

Back « JavaScript numbers

Up to top of page