JavaScript
Color Flipper JavaScript Programming
Color Flipper
Excellent JavaScript Color Flipper Tutorial
Directory Structure
- Create folder in your desired directory named setup
- In the setup folder create files like index.html, app.js, style.css
Creating index.html File
Color Flipper JavaScript Programming
1 We have created an object using an object literal way and it is containing all the hexadecimal digits as object members.
The addEventListener() method takes three arguments:
const hex = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, “A”, “B”, “C”, “D”, “E”, “F”];
2 We have used the getElementById() method of the document object that selects an html element on the basis of ID attribute and store it in the btn variable for later use. Like: const btn = document.getElementById(“btn”);const btn = document.getElementById(“btn”);
3 We have used here querySelector() method of the document object of the HTML DOM that selects an html element on the basis of css selectors and stored it in the color variable for later use. like: const color = document.querySelector(“.color”);const color = document.querySelector(“.color”);
4 The addEventListener() method of the document object of the HTML DOM in JavaScript is used to attach an event handler to an element. An event handler is a function that is called when a certain event occurs, such as a mouse click or a key press.- The first argument is the name of the event. This is the name of the event that you want to listen for such as click or keydown.
- The second argument is the function that you want to call when the event occurs. This function is called the event handler.
- The third argument is an optional Boolean value that specifies whether the event handler should be executed in the capturing phase or the bubbling phase. The capturing phase is the first phase of the event propagation, and the bubbling phase is the second phase.
It has several advantages over other method like onclick and onmouseover.
- It allows you to attach multiple event handlers to the same element.
- It allows you to specify the event propagation phase.
- It works on any event target, not just HTML elements.
btn.addEventListener("click", ()=>{
defining the variable named hexColor for storing the hexColor value.
let hexColor = "#";
A for loop in JavaScript is a control flow statement that repeats a block of code a specified number of times.
It takes the following arguments separated with semicolon.
- initialization: It is an expression that is executed once before the loop starts.This expression is often used to initialize a counter variable.
- condition: It is an expression that is evaluated before each iteration of the loop.If the condition is true the body of loop is executed. If the condition is false, the loop terminates.
- increment: It is an expression that is executed after each iteration of the loop. This expression is often used to increment or decrement the counter variable.
for (let i = 0; i < 6; i++) {
The addition assignment operator += in JavaScript is used to add a value to a variable and assign the result to the variable.
hexColor += hex[getRandomNumber()];
}
We use number of properties of document object to assign the hexColor value to the body element of HTML
document.body.style.background = hexColor;
color.textContent = hexColor;
});
In JavaScript, function is a related group of JavaScript statements that are executed as a single unit. Functions are identical to methods except that they are not associated with any object like document.querySelector()
method that is associated with document object. You must define a function before using it in JavaScript programming lines that make up a function are called the function definition.The syntax for defining a function is as follow.
function name_of_function(parameters) {
statements;
}
function is the keyword then come the name of the function also called identifier after function name, there is set of parentheses like () where you can specify multiple parameters by separating them with comma. Following the parentheses that contain the function parameters is the set of curly braces that contain the function statements. Function statements are the statements that do the actual work of the function.
In the following function, we are going to use some of the JavaScript methods of the Math object like floor and random.
floor(x): Returns the value of x rounded to the next lowest integer.
random(): Returns a random number.
Unlike the Array, Date, and Number classes, the Math class does not contain a constructor. This means that you cannot instantiate a Math object using statement like var addNum = new Math().
Instead, you use the Math object and one of its methods or properties directly in your code like Math.floor(x);
function getRandomNumber() {
return Math.floor(Math.random() * hex.length);
}
A return statement is a statement that returns a value to the statement that called the function.
saani8879
0