r/learnjavascript • u/Outside_Arm8405 • Apr 30 '23
Why isn’t this working as intended?
I want it to draw 3 circles each with different colors. I understand why they’re the same color (I don’t understand how I ever could’ve thought this code would work that way), but I don’t understand why each refresh isn’t providing a new color like it’s providing a new location for my arcs
3
10
u/shekyb Apr 30 '23
you are not transpiling the string, values of color and opacity are not in the final string you re passing to c.strokeStyle
it should be
c.strokeStyle = ‘rgba(${color}, ${color}, ${color}, ${opacity})’
4
u/BrandoobyTV Apr 30 '23
To add onto this. Anything inside of ' ' or " " is treated as a string, and only a string. Whereas if you use tilde, it also treats everything as a string, but also allows you to interject an object using ${ }, where whatever is in the curly braces will be the value of color, instead of simply the word color.
Though with your code I suspect the value of color will be the same for all 3 values. So you may need to do colorR, colorB, and colorG.
3
2
u/Prize_Tea3456 Apr 30 '23
use template string instead
c.strokeStyle = `rgba(${color}, ${color}, ${color}, ${opacity})`;more about template string here
2
u/TheRNGuy Apr 30 '23
Why not just post code here?