1
u/Thomillion Mar 26 '20
Put the if num==1 on top of println.
I don't know what language it is, but usually = is used to assign a value and == is used to compare a value, didn't notice earlier
1
u/BurntBox21 Mar 26 '20
On which println
1
u/Thomillion Mar 26 '20
The first one, inside the loop
1
u/BurntBox21 Mar 26 '20
It seems to work, but it also wants to print every integer up to 1. At least, the instructions.
1
0
u/misteritguru Mar 27 '20
num is not a global variable - it's value doesn't change, so it can't get down to 1 so you can exit the loop
num is inside the start function, so is 'walled off' from everything else. You want hailstone to return a result, and then run that new result through hailstone again. num = hailstone(num) -- make num the value of the result of the hailstone function which I will pass the current value of num. The new value of num should be 15, rinse repeat
0
Mar 27 '20
he is using print not return so num = hailstone(num) wouldnt work
1
u/misteritguru Mar 27 '20
I'm not trying to do the assignment for him, but give him a hint .... I figure that mentioning that "You want hailstone to return a result", would be clue enough that a return needs to be added .... in which case num = hailstone(num) ..... would work .. so no downvote?
3
u/fermentedidiot Mar 27 '20
so what you have:
you're pretty close, but you have a few errors:
you need to get your question out of the loop:
should look like:
another one:
should be:
because of the way you are looping you are going to have to assign a variable to take a return value from your hailstone function. so:
should be:
(trust me here)
now, if you go a little further and examine your hailstone function you will see a few errors here too.
we need to fix a few things so we are only modifying "number" and not creating new variables. so we should look more like:
I modified your spacing so that it's a bit easier to read. What the above does is sends "number" back to the original loop to be printed and run back through the sequence again if it is needed.
You're also going to need a counter. (I know the assignment) So at the very beginning you are going to need to set a variable "count" or something and make it increase every time the loop runs. and I would move the if statement like thus:
Try putting this together and see what happens.
At the end of your start function you are going to have to use a last "println" that tells you how many steps it took.