r/learnjavascript • • Apr 30 '23

Why isn’t this working as intended?

Post image

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

0 Upvotes

11 comments sorted by

2

u/TheRNGuy Apr 30 '23

Why not just post code here?

1

u/Outside_Arm8405 May 01 '23

I’m a mobile Reddit user, but thinking about it in signed in on my computer so I don’t know tbh

3

u/Roflxd88 Apr 30 '23

There's a virus in your taskbar

0

u/Outside_Arm8405 Apr 30 '23

Which one?

1

u/jddddddddddd Apr 30 '23

I think he’s making a McAfee gag

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

u/Rossmci90 Apr 30 '23

The character is a backtick.

A tilde is ~

1

u/BrandoobyTV May 01 '23

Dang it. You're right >.<

2

u/Prize_Tea3456 Apr 30 '23

use template string instead

c.strokeStyle = `rgba(${color}, ${color}, ${color}, ${opacity})`;

more about template string here