The anime never show any mistakes related to the "logic" the Pokemon world employ
Many times in many episodes, they explain many things about their "logic", but most people, don't pay atention to theese details and them sometimes they don't understand how some things could happen.
Well, for that in particular was pretty easy to understand, many times in the anime in many seasons, many characters explain how and why electric moves have no effect in ground Pokemon and how and why they are weak against grass.
One of the best time they explain this, was in this episode: https://www.youtube.com/watch?v=Pj9dxxF-a3c
go for 16:50 minutes
Brock will explain!!
Max ask why pikachu thunderbolt work normally against treecko, since treecko was a grass type and eletric moves was weak against grass types. Brock then will explain that, the eletric move worked normally, because it hit treecko when he was jumping!!
Brock explain that electric moves are weak against grass and have no effect on ground Pokemon, because the eletricity flow throught the body of the Pokemon and hit the ground or tree right away dispersing the energy, them the grass Pokemon take minor damage and the ground Pokemon take no damage!
But when the Pokemon is not on the ground, like hoving or jumping, like in the case of treecko, the move work normally!! Because the Pokemon is not on the ground, so the energy has not to defuse on it!
Knowing about this, Ash and many other characters when facing grass or ground Pokemon, they prefer to hit the Pokemon when he is hoving or jumping, so the electric move work normal.
Then, in the fight against Roxane, Ash waited the right time to hit geodude with a electric move, the time when geodude stop the roll out and start hoving again.
I would like if you mark this as the best anwers to this question, so many other people could understand that question too. Thanks!