I'm having a big problem with my code for my final project for Javascript. Its supposed to be a card counter that is supposed to count how many cards there are and spit out a total, if you're over or under a deck limit, and then spits out all the cards you have, the amount you have and it will tell you and prompt you again if you entered a card amount over the limit (in this case, its 3, though can be changed).
So far, if you enter a double, it enters a loop of "can't do that, enter another amount" even if its a valid amonut. Also, it seems to lose its shit if I enter 0...
On another note, if you somehow bypass the doubles, it seems to take the list and bash the numbers together for the total amonut instead of adding it together
(EX: "You have 03123131213232 cards" instead of "You have 27 cards")
<html>
<head>
<script type="text/javascript">
// Program Name: cardcounter.txt
// Purpose: Chris can't tell if he has enough cards in his deck. We're going to fix that
// Author: Spencer Durette & Benjamin Tofflemire
// Date last modified: 04/15/2015
//Declare Global Variables
var daCardTotal = 0; // Total Number of cards added
var daCard; // The card entered
var cardList = []; // The List of cards that will be displayed at the very end
var numbahOCards = []; // The list of the quantity of each card
var cardQty; // The number of cards entered
var index = 0; // The index used for the final loop in the program to display the total number of cards
var cardQtyCheck = false; // Checks if the card quantity is proper
var BR = "<br />";
var checkIndex = 0; // The index used in finding repeat cards
var cardDouble = false // Checks if there is a card double. This is counted as true if the card found is a double and will skip adding a new card
// as well as adding a new card quantity if this is true
//Set the Card Limit (40 for Yu-gi-oh, 60 for Magic: The Gathering)
var cardMax = 40;
//Declare the maximum number of cards per deck (This program doesn't count cards you can have an infinite number of,
//such as land cards and relentless rats in Magic: The Gathering)
var cardQtyMax = 3;
//Count the card total
var cardCount = function(daGrandTotal) {
if (daGrandTotal > cardMax){
alert("Please remove some cards. You are over the card limit");
document.write("Please remove some cards. You are over the card limit" + BR);
document.write("You have " + daCardTotal + " cards total")
} else if (daGrandTotal < cardMax) {
alert("Please add some more cards. You are under the card limit");
document.write("Please add some more cards. You are under the card limit" + BR);
document.write("You have " + daCardTotal + " cards total")
} else {
alert("You have exactly "+cardMax+" cards in your deck. You have the exact number of cards needed" + BR);
document.write("You have exactly "+cardMax+" cards in your deck. You have the exact number of cards needed" + BR);
document.write("You have " + daCardTotal + " cards total")
}
}
var cardRepeat = function(card) {
//Check if the card added has already been added to the list
while (checkIndex < cardList.length){
if (cardList[checkIndex] == card){
cardDouble = true;
cardQty = prompt("The card you have has already been added. Please enter the added amount (You have " + numbahOCards[checkIndex] + " already added)");
cardQty = parseInt(cardQty);
while (cardDouble == true) {
if ((cardQty + numbahOCards[checkIndex]) >= cardQtyMax){
cardQty = prompt("The card amount entered is invalid. Please enter a valid amount (You have " + numbahOCards[checkIndex] + " added already)");
cardQty = parseInt(cardQty);
} else if ((cardQty + numbahOCards[checkIndex]) < cardQtyMax) {
numbahOCards[checkIndex] = numbahOCards[checkIndex]+cardQty;
break;
}
}
break;
}
//Increment the card index for the card list
checkIndex += 1;
}
//Once the check for doubles is finished, reset the card index for the next card
checkIndex = 0;
}
</script>
</head>
<body>
<script type="text/javascript">
//Welcome the user
alert("Welcome to the card counter program!");
//Run the program
//Prompt the User to enter the first card
daCard = prompt("Please enter the first card","");
//Enter the loop that will have the user consistently enter cards until they entered all of the cards needed
while (daCard != "-1") {
//If the user enters -1, the program stops counting cards
if (daCard == "-1") {
break
} else {
//Check if the card is a double
cardRepeat(daCard);
//Run the regular card adding if the card is not a double
if (cardDouble == false) {
//Add the card to the list of cards by name
cardList[cardList.length] = daCard;;
//Prompt the user for how many cards there are
cardQty = prompt("How many "+daCard+"s are there to add?");
//Check if the card quantity is a valid amount (defined by the variables above)
while (cardQtyCheck == false){
if (cardQty > cardQtyMax) {
//Tell the user that the amount of cards they added is invalid if they entered more then the maximum of allowed cards
alert("You cannot have that many cards in a deck, please enter a valid amount");
cardQty = prompt("How many "+daCard+"s are there to add?");
cardQty = parseInt(cardQty);
} else {
cardQtyCheck = true;
//Add the card quantity if the card total is right
numbahOCards[numbahOCards.length] = cardQty;
cardQty = parseInt(cardQty);
}
}
}
//Add to the grand total of cards that will be displayed at the end
daCardTotal = daCardTotal + cardQty;
//Loop the prompts, this time adding the option to exit with -1
daCard = prompt("Please enter the next card (-1 to exit)","-1");
//Changes the check so the next quantity of cards is checked
cardQtyCheck = false;
}
}
//Count the grand total of cards and checks if the cards are over or under the total amount of cards allowed
cardCount(daCardTotal);
//Displays every card with its respectable number, stopping when the list ends
while (index < (numbahOCards.length)){
if (numbahOCards[index] == "" || cardList[index] == "") {
break;
}
document.write(BR + cardList[index]+" x"+ numbahOCards[index] + BR);
index += 1
}
</script>
</html>
I know its hard to look at, but please bear with me on this. I'll also take any recommendations to make it look... Cleaner? (is that how I would describe it?). I also would like it if someone can explain to me whats going on...