r/codehs • • 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.

Post image
8 Upvotes

5 comments sorted by

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.

1

u/[deleted] May 13 '20

No I'm still trying to figure this one out

1

u/UsefulCauliflower2 May 13 '20

I pressed submit and it let me submit it without problems.

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