Question: Red Green Blue /* Declare and initialize global variables -------------------------------------------------- */ var pageBg = document.querySelector('html'); var sliders = document.getElementsByTagName('input'); var rgb = [0, 0, 0];
/* Declare and initialize global variables
-------------------------------------------------- */
var pageBg = document.querySelector('html');
var sliders = document.getElementsByTagName('input');
var rgb = [0, 0, 0];
/* Event handlers for range sliders
-------------------------------------------------- */
for (var i = 0; i < sliders.length; i++) {
// Loop through the three range inputs and for each one, add an onchange event listener
sliders[i].onchange = function() {
// If an input range slider is moved, grab its id attribute value
var whichSlider = this.getAttribute('id');
// also, grab the numerical value that it is set to
var sliderValue = this.value;
// Declare a new variable to hold the new RGB value that calls a function that updates the global rgb variable, passing in what slider was moved (whichSlider), and its value (sliderValue)
newRgb = changeRgb(whichSlider, sliderValue);
// Call a function that builds a new CSS background-color property (as a string), passing it the updated RGB array (newRgb)
var newCSS = writeCSS(newRgb);
// Directly change the background-color of the page using the new CSS rgb value
pageBg.style.backgroundColor = newCSS;
}
}
/* Functions
-------------------------------------------------- */
// STEP 1: Write a function caclled changeRgb that accepts two parameters, channel and value
Build a switch based on the value of the channel parameter (red, green, or blue)
Inside each case, update the appropriate global rgb array element (0, 1, or 2)
Return the updated rgb array back to the event handler
Write a new function called writeCSS that accepts one parameter, the updated rgb array
//: Declare a new local variable called newColor that will contain the new string that will be used to update the CSS background-color property in the following format: rgb(0,0,0) - initialize it with the start of the string, 'rgb('
// Create a while loop that iterates through the array passed into this function, called newRgb
// STEP 8: For each element of the array, add to the string newColor, the red, green, and blue values, each followed by a comma
Return the string newColor back to the event handler
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
