Modulo block tip


#1

New block: modulo. What is it good for?

First of all it's good for my sweet new project: FizzBuzz https://c.gethopscotch.com/p/y3p9owr0m

But it's super useful for other stuff too.

One good example:

You want a switch. On or off

Before modulo:

With modulo:

Much cleaner.

What other ideas do people have?

Also play my project and see if you can make your own version. It's a classic coding test :+1::wink:


#2

Well, some more help is this:
Modulo just finds the remainder of a division.
9 divided by 3 would be 0 because there is no remainder.
But if you were to to 7 divided by 2, there would be a remainder of one.
So with that switch, any division by 2 that divides perfectly into a whole number would trigger the X%2 = 0.
4%2 would get zero, but 5%2 would get one. 7%2=1, [evennumber]%2=0. With X%2 there is one way or the other. So it is a two way switch.
Someone please tell me if I had inaccuracies in that.


#3

Thanks for that.
I was thinking what's the point unless someone's using it to do their school homework.
I still prefer setting values for on/off switches though


#4

With modulo you can have a switch for every 3rd, 4th, 5th...whatever. Like if you wanted to generate a new bad guy every 11 seconds in your game.


#5

I've never used it so my current coding habits are are hard to break.
Can you post an example of the Maths code to do what you just mentioned?


#6

sure give me a quick second...


#7


#8

Is this what you mean?
A clock that does something every five seconds:
The top rule is when seven is seven.


#9

yupyupyup. Looks good.


#10

I will use some modulo code in an update to my Jewel Thief game.
@Stradyvarious I will post it on the topic for the point towards sensor code.
@RodeoHero you will get credit.


#11

Is a number going up in value continually being divided by 5 and when there's no remainder something happens?

I'll try something out and see if I can use modular successfully


#12

Yea, although the "something happens" is just because we put when the remainder was = 0 as the condition in our Check once if.


#13

You could also check when it = 1, 2, 3, 4, 5 or even 6 (although that will never happen in this case)


#14

Another cool use for it is to "wrap" values. If you do X % "some max value" and then count up by 1, when x hits the max value, X % "some max value" turns back to 0.

An example of this is a clock. Every time it gets to 60 seconds it goes back to 0 seconds.

time = some number of seconds.
time % 60


#15

I found these two things to help me:

https://betterexplained.com/articles/fun-with-modular-arithmetic/


#16

I did this.
Every time the modular = o a click sound happens.
I can see for a RPG this would be good for increasing the XP level

Thanks @RodeoHero
You taught me something new today.