Question: JAVASCRIPT The top-left (initially red) box,, when clicked , will change its background and text colours (per the details given in this assignment) each time

JAVASCRIPT

The top-left (initially red) box,, when clicked, will change its background and text colours (per the details given in this assignment) each time it is clicked.

Writing the box1_click(evt) Function

Start by writing the stub for this function:

function box1_click(evt) { // NOTE: The code to write here is described below. } 

Recall your knowledge about bitwise operations from C (i.e., bitwise-AND, '&', in particular). The variable last_color will hold a value between 0 and 7 (i.e., binary: 000 to 111) where the bits represent whether or not there is red, green, or blue colour. Thus, the code for this function is to (i) reset the value to 0 if last_color == 8, (ii) ++last_color each time this event occurs, and (iii) to properly set the CSS background-color and color attributes of box1. (The background-color attribute will be set to the colour determined by last_color and the color attribute will be set to the complementary colour of the background-color attribute.)

Write the code to do the following in the order listed:

  • If last_color == 8, then set last_color to 0 (zero).
  • Increment last_color with ++.
  • Declare a variable called color_to_set with var and set its initial value to "#".
    • NOTE: The "#" is the "#" character that appears before hexadecimal CSS colour values.
  • Determine the amount of red with: (last_color & 4) ? '0' : 'F'. Append such to color_to_set.
    • String appends in JavaScript can be done using the + operator (if the left-hand side is a string).
  • Determine the amount of green with: (last_color & 2) ? '0' : 'F'. Append such to color_to_set.
  • Determine the amount of blue with: (last_color & 1) ? '0' : 'F'. Append such to color_to_set.
  • Declare a variable called bcolor_to_set with var and set its initial value to "#".
  • Determine the amount of red with: (last_color & 4) ? 'F' : '0'. Append such to bcolor_to_set.
  • Determine the amount of green with: (last_color & 2) ? 'F' : '0'. Append such to bcolor_to_set.
  • Determine the amount of blue with: (last_color & 1) ? 'F' : '0'. Append such to bcolor_to_set.
  • Assign this.style.backgroundColor to bcolor_to_set.
  • Assign this.style.color = color_to_set

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!