Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Concepts

Authors: David M Kroenke, David J Auer

6th Edition

0132742926, 978-0132742924

More Books

Students also viewed these Databases questions

Question

=+What sanctions are available to employers

Answered: 1 week ago

Question

=+ If strikes occur, are they legally regulated?

Answered: 1 week ago

Question

=+industrial action Under what circumstances can unions strike?

Answered: 1 week ago