Jump to content

JavaScript Code Help

Featured Replies

Hi, I am working on an assignment for my CSC class and I am having trouble with the code. I have an idea on what I would like to do. The purpose of the assignment is to make a simple game of craps. First time you roll if you land on a 7 or 11, you win automatically. If you roll on 2, 3, or 12, automatic loss. Anything else keep rolling until you and on a 7, then you automatically lose. If you can look at my code and let me know what you think of it, I truly appreciate it.

 

// JavaScript source code

// First going to create the bet function
function BtnBet_onclick() {
//create variables
var die1;
var die2;
var rolls = 1;
var bet = document.getElementById("txtBet").value;
var results = "";
var NetMoney;
var MoneyWon
var MoneyLoss;
alert("1");
// Roll 'em, Boys!
die1 = (Math.floor(Math.random() * 6) + 1);
die2 = (Math.floor(Math.random() * 6) + 1);
var sum = die1 + die2;
sum = die1 + die2;
results += die1 + " + " + die2 + "==>" + sum + "\n";
document.getElementById("txaResults").value = results;
switch (results) { //This is for the first roll
case 7:
case 11:
alert("Winner!")
MoneyWon += bet;
NetMoney += MoneyWon;
document.getElementById("txtmw").value = MoneyWon;
document.getElementById("txtnm").value = NetMoney;
break;
case 2:
case 3:
case 12:
alert("Sorry, try again!");
MoneyLoss += bet;
NetMoney = NetMoney - MoneyLoss;
document.getElementById("txtml").value = MoneyLoss;
document.getElementById("txtnm").value = NetMoney;
break;
case 4: //This is where I am thinking about making another loop.
case 5:
case 6:
case 8:
Case 9:
case 10:
alert("yo");
break;
}
}

Archived

This topic is now archived and is closed to further replies.

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.