// H1 ELEMENTS
const h1 = document.querySelectorAll('.h1-container h1');
const h1_btn = document.getElementById("h1-random");
// h1 button color click
h1_btn.addEventListener("click", () => {
for(let i = 0; i < h1.length; i++) {
let randRGB = Math.floor(Math.random() * 255 + 1);
let randRGB2 = Math.floor(Math.random() * 255 + 1);
let randRGB3 = Math.floor(Math.random() * 255 + 1);
h1[i].style.color = `rgba(${randRGB},${randRGB2},${randRGB3},1)`;
}
});
// LI button color click
const a_TAG = document.querySelectorAll('.main-n-item a');
const hexValues = [0,1,2,3,4,5,6,7,8,9,"A","B","C","D","E","F"];
const li_BTN = document.querySelector('#li-random');
let hexString = "#";
li_BTN.addEventListener("click", () => {
function valueLoop(){
for(let i = 0; i < 5; i++) {
let value = Math.floor(Math.random() * 15 + 1);
return value
}
}
for(let i = 0; i < a_TAG.length; i++){
a_TAG[i].style.color = `${hexString}`+
`${hexValues[valueLoop()]}`+
`${hexValues[valueLoop()]}`+
`${hexValues[valueLoop()]}`+
`${hexValues[valueLoop()]}`+
`${hexValues[valueLoop()]}`+
`${hexValues[valueLoop()]}`;
}
/* Original Attempt
for(let i = 0; i < a_TAG.length; i++){
a_TAG[i].style.color = `${hexString}${hexValues[valueLoop()]}${hexValues[valueLoop()]}${hexValues[valueLoop()]}${hexValues[valueLoop()]}${hexValues[valueLoop()]}${hexValues[valueLoop()]}`
}
*/
});
-
It would help to know what is being critiqued. Please provide a description of the project, ideally with a link to it being hosted somewhere (CodePen, Github, etc).
-
Variable names in JavaScript should be camelCase, not snake_case.
-
The variable names you use aren’t very descriptive.
-
valueLoop
should be defined outside of the event listener. -
Why does
valueLoop
exist at all?
let hexString = "#";
- It’s great that you named this value. Since it’s not being changed, switch to
const
. It’s also good practice to name constant values in all upper case, egHEXSTRING
.
Thanks for the criticism. I’ll be sure to be more descriptive next time, and name my variables consistently in camelCase .