-2

Trying to make a blackjack game my first button works fine but trying to pass the hit and stand isn't working just says not defined.

I'm trying to pass the onclick playerMove("stand") and playerMove("Hit") to the playerMove(a) function at the bottom but wont work.

<body>
    <div> <!--Betting function and buttons for the game-->
        <div><h1> BlackJack. Beat The Dealer</h1></div>
        <div class ="cash">Total £<span id="pounds">1000</span></div>
        <div>Stake £<input type="number" id="mystake" value="0" min="1">
        <div id ="msg"></div>
        <div id="start">
            <button id ="startbtn" type="button" onclick="Start()" class = "btn">Start Game</button>
    </div>
    <div id ="result">
        Dealers Hand : <span id="dealValue"></span>
        <div id ="dealerHold"></div> <!--placements for dealer hand-->
        Players Hand : <span id="playerValue"></span>
        <div id = "playerHold"></div> <!--placements for player hand-->
        <div id="playerMoves">
            <button id="btnstand" type="button" onclick='playerMove("stand")' class="btn">Stand</button> <!-- buttons for the main blackjack functions -->
            <button id="btnhit" type="button" onclick='playerMove("hit")' class="btn">Hit</button>
    </div>
    <div>
        <button id="btndeal" type="button" onclick="newDeal()" class="btn">Deal</button>
    </div> <!-- once cards are dealt this button will dissapear -->
</div>

function playerMove(a){
        console.log(a);
        switch(a){
            case 'hit':
                playaCard(); // if hit is passed through then we run playacard function
                break;
            case 'stand': //if stand is passed through we end the players game through function
                playend();
                break;

        }
    }

3 Answers3

0

Any JavaScript code must be defined within a block. That does not appear to be the case above.

Simply adding <script> before and </script> after the definition of playerMove should do the trick.

mikeboharsik
  • 110
  • 5
  • Hi thank you for the answer it's all in script tags theres about 10 other functions on there so it was too much to post on here so i just took the snippet of the playerMove function, nto sure how to post all the code – datway222 Aug 22 '20 at 20:17
  • Uncaught referenceerror: playerMove is not defined at HTMLButtonElement.oneclick index.html:119 line 119: if you have discord i could quickly screenshare if thats ok? – datway222 Aug 22 '20 at 20:54
-1

It is hard to fix that is not broken... Your code works normal. See this code snippet (I've added some empty mock functions for testing purpose):

// Some mocks (just to ensure that other code works)
playaCard = () => console.log('playaCard');
playend = () => console.log('playend');
newDeal = () => console.log('newDeal');
Start = () => console.log('Start');

// Original function
function playerMove(a){
        console.log(a);
        switch(a){
            case 'hit':
                playaCard(); // if hit is passed through then we run playacard function
                break;
            case 'stand': //if stand is passed through we end the players game through function
                playend();
                break;

        }
    }
<body>
    <div> <!--Betting function and buttons for the game-->
        <div><h1> BlackJack. Beat The Dealer</h1></div>
        <div class ="cash">Total £<span id="pounds">1000</span></div>
        <div>Stake £<input type="number" id="mystake" value="0" min="1">
        <div id ="msg"></div>
        <div id="start">
            <button id ="startbtn" type="button" onclick="Start()" class = "btn">Start Game</button>
    </div>
    <div id ="result">
        Dealers Hand : <span id="dealValue"></span>
        <div id ="dealerHold"></div> <!--placements for dealer hand-->
        Players Hand : <span id="playerValue"></span>
        <div id = "playerHold"></div> <!--placements for player hand-->
        <div id="playerMoves">
            <button id="btnstand" type="button" onclick='playerMove("stand")' class="btn">Stand</button> <!-- buttons for the main blackjack functions -->
            <button id="btnhit" type="button" onclick='playerMove("hit")' class="btn">Hit</button>
    </div>
    <div>
        <button id="btndeal" type="button" onclick="newDeal()" class="btn">Deal</button>
    </div> <!-- once cards are dealt this button will dissapear -->
</div>

What are you trying to achieve?

Anton
  • 2,669
  • 1
  • 7
  • 15
  • Hi Anton thank you for the response, this is what's confusing to me my code doesn't seem broken in an insolated instance it runs well but i think i've made some indenting errors somewhere or something which isn't allowing the function to be called properly. Would you by any chance be able to see exactly what it is if i send you the .html? I'd really appreciate it – datway222 Aug 22 '20 at 22:23
  • @datway222 I don't know where do you want to send your HTML. I think you should first try to simulate in https://jsfiddle.net/ (or in similar online HTML+JS testing tool). Another options for you is to use "code snippet" here on this site as I do. – Anton Aug 22 '20 at 23:42
  • @datway222 When you will be able to reproduce your error in any of online tools like jsfiddle, you can ask here about the fix. – Anton Aug 22 '20 at 23:43
  • Hey I appreciate the help managed to get it sorted it was a scoping error thank you – datway222 Aug 23 '20 at 00:54
-2

Wrap up the JavaScript code with script tag. I hope it will work

  • Hi thank you for the answer it's all in script tags theres about 10 other functions on there so it was too much to post on here so i just took the snippet of the playerMove function, nto sure how to post all the code – datway222 Aug 22 '20 at 20:17