Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript JavaScript Loops, Arrays and Objects Simplify Repetitive Tasks with Loops Introducing Loops, Arrays and Objects

Tadjiev Codes
Tadjiev Codes
9,626 Points

Hi, Fellows, My code with Loop has an error as the variable cashOnHand doesnt get decremented? What would be the problem

Hi, Fellows, My code with Loop has an error as the variable cashOnHand doesn't get decremented even though I enter the values? What would be the problem? Here's the link in Codepen for my JS code. https://codepen.io/mukhammad-tadjiyev/pen/eYNOKjy

I would really appreciate it If somebody could help me out.

Thanks and regards, Tadjiev

3 Answers

I made a workspace with your code and a number of changes

1) Changed to a while loop. Otherwise your loop could end at some arbitrary time when i > cashOnHand . Now you can keep playing as long as you have cash.

2) The bet prompt now uses the cashOnHand variable to display the actual value of cashOnHand. Your code decrements cashOnHand but displays the string "Cash on Hand: $1000"

3) If an invalid bet is made bet is not set to zero.

4) The rest of the code that includes the actual game is now part of the else statement. This is so the code executes only if a valid bet is made.

5) Similar to the bet prompt, the win prompt now displays the actual win "You won " + (bet * WINRATE)

6) If there is cash remaining the confirmation to continue is now evaluated and the game ends if the user chooses to do so.

7) This final output is displayed outside the loop.

I didn't check this thoroughly nor is this the best solution. For example what if the initial bet prompt is cancelled or something besides a number is entered? There are improvements to be made.

Tadjiev Codes
Tadjiev Codes
9,626 Points

Hey KRIS, Thank you so much for taking the time to sort out this problem for me. But It was a wonderful solution compared to mine. Thank you so much for making my day and explaining all the mistakes. Now the Slot MAchine works YAY as supposed to. But if I want this invalid bet to be set on 0 then how I would do it? I would have to set the variable to = 0 ?

3) If an invalid bet is made bet is not set to zero.

And, Can I create workspaces as u did but if I'm on the basic subscription?

Thank you so much.

I really appreciate it.

Have a Good night...

You could set bet to 0 but it isn't necessary since the game code doesn't execute if the value is less than zero or greater than cash on hand (the logic should probably be less than or equal to zero so zero bets aren't allowed). However on your next line you display "Wrong bet of $ " + bet + " Try again" and in order to display the bet made you'd have to leave the value unchanged.

I think anyone can create up to 100 workspaces before you have start deleting the ones you don't want to keep. Just click Workspaces at the top the choose New+. Sharing a workspace is done by creating a snapshot. In an individual workspace click the camera icon in the upper right corner, then 'Take Snapshot', then post the link created in the forums. Other students can then open (fork) their own copy to do as they please but it's nice because all the files and environment will be there to use.