Needing Help With My First JavaScript script

Discussion in 'Javascript' started by docrobjob, Mar 22, 2018.

  1. docrobjob

    docrobjob

    Joined:
    Mar 22, 2018
    Messages:
    1
    Likes Received:
    0
    I'm making my first code and it's a card game I made up, so the first part of the script is to shuffle a deck. I'm making the non graphic version of the game until I can learn to use a SpriteSheet and cards. The script is having trouble with the function tallypoints() and I don't know why. If I remove the If statement that is inside the for loop, then it at least exits the loop but doesn't run down to the line I am wanting it to. My only goal at this point is to get it to run all the way to the document.write so that it displays the cards selected and a score that is tallied. Without going into the specifics of the game--can you tell me what to do to get this to run and any tips that turn three or four lines of code into one, for example. If you desire to complete the whole code so I can see what it takes I don't mind, I put the last directive of the code in the comments below. Thank you for your help.

    <head>
    <script>
    var cards = ["A♠", "K♠", "Q♠", "J♠", "10♠", "9♠", "8♠", "7♠", "6♠", "5♠", "4♠", "3♠", "2♠", "A♣", "K♣", "Q♣", "J♣", "10♣", "9♣", "8♣", "7♣", "6♣", "5♣", "4♣", "3♣", "2♣", "A♥", "K♥", "Q♥", "J♥", "10♥", "9♥", "8♥", "7♥", "6♥", "5♥", "4♥", "3♥", "2♥", "A♦", "K♦", "Q♦", "J♦", "10♦", "9♦", "8♦", "7♦", "6♦", "5♦", "4♦", "3♦", "2♦"];
    var selectednumbers = []
    var newdeck = []
    var randomcard = 0
    var flocknumber = 1
    var flockzero = false
    var multiplyby = 1
    var c = 0
    var x = 0
    var jackmult = 1
    var fncount = 0
    var testarray = []

    /*This is the process to shuffle a deck */
    function checkCard() {
    for (x = 0; x <= selectednumbers.length - 1; x++) {
    if (randomcard == selectednumbers[x]) {
    randomcard = (Math.floor(Math.random() * 52));
    checkCard.call(this);
    }
    }
    }

    while (selectednumbers.length <= 53) {
    randomcard = (Math.floor(Math.random() * 52));
    checkCard();
    selectednumbers.push(randomcard)
    newdeck.push(cards[randomcard])
    /* document.write(cards[randomcard]+"<br>") */
    }
    /* This is the function that runs after you click the button: */
    function tallypoints() {

    flocknumber = document.getElementById("mySelect").options[document.getElementById("mySelect").selectedIndex].index + 1;
    /*This next line sets a counter that will make the for loop run and check every card you selected to see if it is a Jack
    if it is a Jack you get zero points for this round */
    fncount = (c + flocknumber)
    for (c = c; c != fncount; c++) {

    testarray.push(newdeck[c])
    //document.write(fncount)
    if (newdeck[c].charAt(0) = "J") {
    multiplyby = (multiplyby * 0);
    flockzero = true;
    }
    }
    c = c + 1
    /*this will check the next card in the pile after the cards you selected and see if it is a Jack. If it is a Jack then you get a score equal to 5 times the number of cards you picked. If it is any other card you get one point for each card you picked. */
    if (newdeck[c].charAt(0) = "J") {
    jackmult = 5
    } else {
    jackmult = 1
    }
    if (flockzero = false) {
    multiplyby = 1
    }

    score = score + (flocknumber * multiplyby * jackmult)
    multiplyby = 1
    document.write(testarray + " score:" + score)

    flockzero = false


    /*Some variables are being reset to their default position, but some variables remain the same so that the next click will repeat the process. Instead of document writing which clears the window--I would like to have the output placed under the select box so that it remains in the window for the next turn. But, I don't know how to do that Also when 40 cards have been selected which means c=40 I need to remove the 12 option or index 11 from the select box and when 41 cards have been selected I need to remove the 11 and 12 options or 10 and 11 index from the select box repeating this for 42 cards/remove 10 11 and 12 repeating this up to 51 cards and if c=51 then card number 51 must be checked to see if it is a J because the score will increase by zero if card 51 is a Jack but if it isn't then card 52 needs to be checked to see if it is a Jack and if it is a Jack and 51 isn't then score will increase by 5 but if card number 52 is not a jack and 51 is not a jack then the score increases by 1 */
    }

    </script>
    </head>
    <form>
    <select id="mySelect">
    <option>1</option>
    <option>2</option>
    <option>3</option>
    <option>4</option>
    <option>5</option>
    <option>6</option>
    <option>7</option>
    <option>8</option>
    <option>9</option>
    <option>10</option>
    <option>11</option>
    <option>12</option>

    </select>
    <input type="button" onclick="tallypoints()" value="Herd Sheep">
    </form>
     
    docrobjob, Mar 22, 2018
    #1
    1. Advertisements

  2. docrobjob

    laphatize

    Joined:
    Mar 23, 2018
    Messages:
    2
    Likes Received:
    0
    If you want it to run the entire script, I suggest making it a function. Make sure you call the function too.
     
    laphatize, Mar 23, 2018
    #2
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.