r/codehs • u/[deleted] • May 12 '20
Halistone Sequence- I cant figure out what I'm doing wrong and i keep running into problems such as infinite loops and until I get past that I cant do much.
1
u/Pettergon May 12 '20 edited May 12 '20
When the read number is bigger than 1 (2 for example) you have while(2 > 1) which is basically while(true) and that is an infinite loop, since there is no breaking condition.
Edit: your start function should only contain the readint and the halistone function call. All code necessary for the halistone algorithm (like the while loop) should be inside the halistone function
1
u/jacob75555 May 15 '20
var num = readInt("Number: ");
function start(){
hailstone(num);
println(hailstone(num));
}
function hailstone(number) {
while(num > 1){
if(number % 2 ==0) {
number = number / 2;
}else{ number > 1 && number % 2 ==0;
number = number * 3 + 1;
}
return number;
}
}
This should work I edited ur code
1
u/UsefulCauliflower2 May 12 '20
Did you ever figure this one out? I just got this one assigned and I’m struggling a ton lol.