The Penny Doubling Challenge

The Penny Doubling Challenge

The Penny Doubling Challenge

  • CodeCamp Admin

  • 4 minute read

The Penny Doubling Challenge

Hello, everyone. Rob here with PreCodeCamp.com. And in this video, we have an interesting beginner challenge for you JavaScript learners out there. And it's going to be based on and hopefully, you've heard of something like this before where whether you are willing to take one million dollars right now, or would you rather wait on a penny doubling each day for up to thirty days? And so we're gonna go ahead and challenge you with this. Feel free to pause the video, and then we're going to go ahead and go over it with the solution. So hopefully, you pause the video. Hopefully, you get it. Alright? So we're gonna go ahead and go over the solution right now.

The Solution

Taking a look at the code here, the way that we want to approach this is we need some sort of variable outside of a loop to kind of keep track of it for us so we don't have to visually see it doubling each time it's going through the loop. Right? We don't wanna console log that information. We wanna console log the final product. And so for that, we're going to create a variable that will allow us to keep track of the total. Right? And we're gonna start off with just a penny. That's what we wanna start off with.

Now the next thing that we wanna do is we want to write out our for loop. Okay? Now the for loop, of course, is gonna start with that keyword for, and then it takes three parts to this. Right? Is what are we initializing it at? And then what is our counter going to be set at? What is our conditional gonna be in the middle? Right? And then what do we want to add each time to the loop in order to move on to the next iteration. Right? And so for that, we're just gonna go with a simple for loop just like this. And so you can see right here, we are initializing it to one. Again, the semicolon is very important because that's what's gonna separate these, three parts for us. And then how long do we want to do this loop for? And then this is our I plus plus. Right? So each time we go through the loop, we are going to go ahead and increase that count. Alright?

So with that being said, we have this one right here, and this is like to the power of two. Right? And so it's going to double it each time. Right? It starts off at a penny, and then it moves on and doubles. It doubles each time for three times. Now if we were to go ahead and run this right now, we're not gonna get anything, And that's because we need to actually console log the total at the end of the loop. Right? And so let's go down here on my line seventeen, and we'll just do a console log here of the total. K. And we don't need this extra stuff that Copilot put in there for us, but we can go ahead and save that. And then if we want, we can go head nets.

So there we go. That is the number. Right? And if we were looking at this from a dollar point of view, a dollar perspective, that would be ten million dollars by doubling that penny. So, of course, hopefully, if you were given that choice that we would actually choose the penny to double each time in order for us to make more money.

Conclusion

Hopefully, you enjoyed this tutorial. If you did, make sure that you give it a like and a subscribe, and we'll see you next time.