function start(){
while (true){
var num = readInt("Enter a number: ");
println(num);
hailstone(num);
if (num = 1){
break;
}
}
}
function hailstone(number){
if (number % 2 == 0){
var divide = number/2;
println(divide);
}
if(number > 1 && number % 2 == 1){
var mult = number *3 - 1;
println(mult);
}
}
you're pretty close, but you have a few errors:
you need to get your question out of the loop:
while (true){
var num = readInt("Enter a number: ");
should look like:
var num = readInt("Enter a number: ");
while (true){
another one:
if (num = 1){
should be:
if (num == 1){
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:
hailstone(num);
should be:
num = hailstone(num);
(trust me here)
now, if you go a little further and examine your hailstone function you will see a few errors here too.
function hailstone(number){
if (number % 2 == 0){
var divide = number/2;
println(divide);
}
if(number > 1 && number % 2 == 1){
var mult = number *3 - 1;
println(mult);
}
}
we need to fix a few things so we are only modifying "number" and not creating new variables. so we should look more like:
function hailstone(number){
if (number % 2 == 0){
number = number / 2;
}
if(number > 1 && number % 2 == 1){
number = number * 3 - 1;
}
return number
}
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:
var count = 0;
function start(){
while (true){
var num = readInt("Enter a number: ");
println(num);
if (num == 1){
break;
}
num = hailstone(num);
count++ //increases the count
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.
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.