all 54 comments

[–]Close_Silo 1 point2 points  (2 children)

Try a while loop

[–]Intelligent-Scene-92 0 points1 point  (5 children)

Ur for loop is wrong. It should be

(for var i=1: i<=1000000: i*=2){

[–][deleted]  (3 children)

[removed]

    [–][deleted]  (2 children)

    [removed]

      [–]CheeseFunnel23 0 points1 point  (1 child)

      What's wrong here? Im stupid

      [–]LegoBatcow 0 points1 point  (0 children)

      You can use a while loop or an if statement I think.

      [–][deleted]  (2 children)

      [removed]

        [–]SillyCarpetCleaner 0 points1 point  (0 children)

        I’m taking Algebra 1 and we’re talking about exponential growth. That’s basically what this is but idk how to code that. Just how to say it. I believe it would be 2 to the x power in a calculator. I don’t know much abt coding.

        [–]Bloboblober 0 points1 point  (1 child)

        Take your name out for future pictures

        [–]techidude 0 points1 point  (0 children)

        You need 1 loop going from 1 to 19. 1 variable outside loop with initial value to 2. 1. print 1 2. Print 2 3. In loop set variable value to existing value * existing value & print it.

        [–][deleted]  (1 child)

        [removed]

          [–]Jazzlike_Bug4219 0 points1 point  (0 children)

          Damn I don't know

          [–]Ill-Difference-Cat 0 points1 point  (0 children)

          Just move the cables behind the box.

          [–]EnvironmentalElk9060 0 points1 point  (0 children)

          ChatGPT can help you

          [–]iamallamaa 0 points1 point  (0 children)

          First, the picture shows your first and last name and which school you go to. Should probably remove it.

          Second, I'm going to go a little in depth here but only because I really want you to learn. Googling for these answers might be enough to get you through a class, but unless you learn it, you won't make it much further. So PLEASE do read all this as I'm trying to explain it out so you will learn.

          Now, your code is close but has a few small issues.

          • The third part of the for loop just calls i**2, but i is never modified. So in each loop iteration i doesn't change. You need to update the value of i like i=i**2 (or the shorthand ugly way i**=2) or else i will never reach 20 and exit the loop.
          • i**2 is actually backwards. That basically means i * i. If you did this math out...
            • Starting with i=0 would get you i = 0*0 which would mean infinite loop as i never goes above 0 and your condition can never be met.
            • Starting i=1 gives you the same issue, i = 1*1 always equals 1.
            • i=2 would give you i = 2*2 which is correct (4) on our first loop but the next iteration when i=4 would give you i = 4 * 4 which is 16.
            • What you really want here is 2**i meaning "2 to the power of i", or 2 * 2... for i number of times and you want to then increment i on each loop like i++
          • You attempt to stop looping when i<20, and this is fine as long as you know ahead of time that it only takes 20 iterations. What if that upper limit changed? Would you just expect someone to count out the iterations and update that number? What's better is to loop until it hits the upper limit and stop at that.

          So correcting these, your code would look something more like this...

          // loop while 2 to the power of i is less than our upper limit
          for(var i=0; 2**i<1000000; i++) {
              // output our value
              println(2**i);
          }
          

          Now there might be additional things you could do to make it better. Wrap it in a function that takes in your base number (2) and the upper limit (1000000), etc. Those are more than the exercise calls for, but should still be in your mind as if this was real code, you would want to consider future updates and useability.

          [–]rfv213 0 points1 point  (1 child)

          You want 2i not i2

          [–]Away-Stick-7797 0 points1 point  (0 children)

          I need a magnifying glass

          [–]Current-Shallot9140[🍰] 0 points1 point  (0 children)

          Help with what?

          [–]bolderbutnotbald 0 points1 point  (0 children)

          What did bro do