r/learnjavascript • u/kanavkowhich • Mar 03 '26
Hi! Help pls
I have this code snippet that refuses to work properly. The idea is that the function should make a random div slightly greener, all the id's are valid and work fine as long as I use numbers instead of variables for r, g and b.
function ohwow(){
var sumth=Math.floor(Math.random()*10000+1)
const pixel = document.getElementById('pixel' + \${sumth}`)`
const currentColor = window.getComputedStyle(pixel).backgroundColor;
const rgbValues = currentColor.match(/\d+/g).map(Number);
let [r, g, b] = rgbValues;
g = Math.min(g + 10, 255);
pixel.style.backgroundColor = \rgb(${r},${g},${b})`;`
}
I also asign an rgb property to each 'pixel' when it's created :b.style.setProperty("background-color", \rgb(0, 0, 0)`);`
View all comments
Show parent comment
1
u/kanavkowhich Mar 03 '26
nope. I've tried using greater values like g+100 to see the difference better, still doesn't work