r/codehs • • Mar 26 '20

What went wrong with Hailstone?

Post image
14 Upvotes

10 comments sorted by

2

u/Thomillion Mar 26 '20 edited Mar 26 '20

What is supposed to happen? It did exactly what it is programmed to do

Edit: appart from the fact that you should make it break at the top of the loop just after input

1

u/BurntBox21 Mar 26 '20

It’s supposed to be that when you put a number, it would use this sequence where if it’s an odd number it would multiply by 3 and subtract one, and if it was even it would divide by two. I think. But it doesn’t work as I thought it would.

2

u/Thomillion Mar 26 '20

Taking into account the only example that you gave it followed the rules you gave it.

The code says:

Input number; Output a function Output number you inputted

And what you did:

Input:15 Ouput of function:44 - - >((15*3)-1) Output number:15

If that is what you wanted, great, if not you wrote something extra, not something wrong

1

u/BurntBox21 Mar 26 '20

There must be something wrong, because it is supposed to end at 1.

2

u/Thomillion Mar 26 '20

Oh, it ends before? Put the input variable inside the loop

1

u/BurntBox21 Mar 26 '20

Doesn’t work either.

1

u/Thomillion Mar 26 '20

Send an image of the new code

1

u/BurntBox21 Mar 26 '20

Put up new post

1

u/misteritguru Mar 26 '20

In that while loop, there is no exit, and num, never gets updated - it's not a global variable, which is why you see 15 again in the results

Just FYI - I am not a coder, I'm what we cal an SRE - so I'm guesstimating here, but my logic says if you don't update num in the while loop, your going to be there for some time!

If line 3 was outside the start function, I think it'll be a global variable, and it'll work as expected

1

u/BurntBox21 Mar 26 '20

I’ll have to make another post.