r/learnjavascript • • 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)`);`

3 Upvotes

7 comments sorted by

View all comments

Show parent comment

1

u/Pocolashon Mar 03 '26

Console log the rgb string you are setting. What is it showing?