# Random Rotation of an Image

**URL:** <https://forum.code.org/t/random-rotation-of-an-image/37335>\
**Category:** CS Principles\
**Tags:** csp-unit-4\
**Created:** [December 6, 2022, 8:55pm UTC](https://forum.code.org/t/random-rotation-of-an-image/37335 "2022-12-06T20:55:16Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![pitchernikki](https://avatars.discourse-cdn.com/v4/letter/p/ee7513/32.png) [@pitchernikki](https://forum.code.org/u/pitchernikki)\
**Post date:** [December 6, 2022, 8:55pm UTC](https://forum.code.org/t/random-rotation-of-an-image/37335/1 "2022-12-06T20:55:16Z")

</div>

setStyle(“image1”,“transform:rotate(180deg);”);  
This will rotate the image 180 degrees, but what if I wanted to have the image rotate a random number of degrees each time? Is there an easy way to do that without having to create a random number from 0-360 and attach it to deg in the above statement?

---

<div class="post-metadata">

**Author:** ![terence.stone25](https://sea2.discourse-cdn.com/flex016/user_avatar/forum.code.org/terence.stone25/32/5124_2.png) [@terence.stone25](https://forum.code.org/u/terence.stone25)\
**Post date:** [December 7, 2022, 12:44pm UTC](https://forum.code.org/t/random-rotation-of-an-image/37335/2 "2022-12-07T12:44:16Z")

</div>

@pitchernikki, this is a great question. I think assigning a random number to a variable and attaching it to the deg is your best option. I will pass this on to the curriculum team for clarity.

---

<div class="post-metadata">

**Author:** ![pitchernikki](https://avatars.discourse-cdn.com/v4/letter/p/ee7513/32.png) [@pitchernikki](https://forum.code.org/u/pitchernikki)\
**Post date:** [December 7, 2022, 1:57pm UTC](https://forum.code.org/t/random-rotation-of-an-image/37335/3 "2022-12-07T13:57:19Z")

</div>

I tried to do that and I couldn’t get it to work. Please let me know if you find something that does. Thanks.

---

<div class="post-metadata">

**Author:** ![mwood](https://sea2.discourse-cdn.com/flex016/user_avatar/forum.code.org/mwood/32/4941_2.png) [@mwood](https://forum.code.org/u/mwood)\
**Post date:** [December 7, 2022, 2:48pm UTC](https://forum.code.org/t/random-rotation-of-an-image/37335/4 "2022-12-07T14:48:10Z")

</div>

@pitchernikki,

Does this work? Not sure what you mean by not attaching it to deg…

> **[Check out what I made](https://studio.code.org/projects/applab/3rvf504JD5AqtIxb8Ok7qg0jzYCR-OoUym1fOl-0BJw)**
>
> I wrote the code myself with Code.org

Mike

---

<div class="post-metadata">

**Author:** ![jdonwells](https://sea2.discourse-cdn.com/flex016/user_avatar/forum.code.org/jdonwells/32/7900_2.png) [@jdonwells](https://forum.code.org/u/jdonwells)\
**Post date:** [December 8, 2022, 2:51pm UTC](https://forum.code.org/t/random-rotation-of-an-image/37335/5 "2022-12-08T14:51:48Z")

</div>

`setStyle()` takes string arguments. Those arguments are sent to the JavaScript interpreter running in your browser. The one that runs App Lab itself. You must build the string you want to send.

What I would do is create a function for it.

```javascript
function setRotation(id, rotation) {
  setStyle(id, "transform:rotate("+rotation+"deg);");
}

```

Then you can rotate it with just a variable.

---

<div class="post-metadata">

**Author:** ![pitchernikki](https://avatars.discourse-cdn.com/v4/letter/p/ee7513/32.png) [@pitchernikki](https://forum.code.org/u/pitchernikki)\
**Post date:** [December 8, 2022, 3:42pm UTC](https://forum.code.org/t/random-rotation-of-an-image/37335/6 "2022-12-08T15:42:22Z")

</div>

Thank you so much for your help. That works great.
