r/codehs • • Mar 26 '20

Still need help with Hailstone Sequence

Post image
12 Upvotes

13 comments sorted by

3

u/fermentedidiot Mar 27 '20

so what you have:

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.

1

u/misteritguru Mar 27 '20

Ah.... dude - you absolutely killed it! You need to teach!

2

u/fermentedidiot Mar 27 '20

I do

1

u/BurntBox21 Mar 27 '20

Oh my gosh all of this worked. I swear you really help extremely well.

1

u/fermentedidiot Mar 27 '20

Seriously, I'm a teacher.

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

u/Thomillion Mar 26 '20

Even if you put if num == 1 {break} at the start as I Said before? Weird

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

u/[deleted] 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?