dcsimg
www.webdeveloper.com
Results 1 to 4 of 4

Thread: Issue

  1. #1
    Join Date
    Oct 2017
    Posts
    1

    Issue

    I got bored, and I'm attempting to make a BlackJack program. However, I'm getting a weird error.

    Here's my code:
    Code:
    let cardTotal = 0;
    
    let ask = confirm('[Game] Are you ready to start?');
    
    if (ask === true) {
      start();
    } else {
      end();
    }
    
    function generateCardNumber() {
      let numbers = [
        '2',
        '3',
        '4',
        '5',
        '6',
        '7',
        '8',
        '9',
        '10',
        'jack',
        'queen',
        'king',
        'ace',
      ];
      return numbers[Math.floor(Math.random() * numbers.length)];
    }
    
    function generateCardType() {
      let types = ['hearts', 'diamonds', 'spades', 'clubs'];
      return types[Math.floor(Math.random() * types.length)];
    }
    
    function getWorth(card) {
      if (card > 0 && card < 11) {
        return card;
      } else if (card === 'jack') {
        return 10;
      } else if (card === 'queen') {
        return 10;
      } else if (card === 'king') {
        return 10;
      } else if (card === 'ace') {
        return 11;
      } else {
        return 0;
      }
    }
    
    function getSubTotal(cardValue) {
      cardTotal = cardTotal + cardValue;
      return cardTotal;
    }
    
    function start() {
      confirm = confirm('[Blackjack] Click OK to generate your first card!');
      if (confirm === true) {
        newCard = generateCardNumber();
        worth = getWorth(newCard);
        total = getSubTotal(worth);
        console.log('[Blackjack] You picked a ' + newCard + ' of ' + generateCardType() + '. New total: ' + total);
        if (total > 21) {
          confirm('[Blackjack] You lose!');
        } else if (total === 21) {
          confirm('[Blackjack] You win!');
        } else {
          confirm('[Blackjack] Click OK to pick another card!');
          if (confirmtwo === true) {
            start();
          }
        }
      }
    }
    This is the error I get
    Code:
    TypeError: confirm is not a function
        at start:68:7
        at eval:6:3
        at eval
        at n.<anonymous>
    Any help is appreciated. Thanks!

  2. #2
    Join Date
    Mar 2007
    Location
    localhost
    Posts
    5,256
    take out all the "let" declarations, unless you are uning strict mode, they wont work and if memory serves me, not all browsers will support let or const.
    --> JavaScript Frameworks like JQuery, Angular, Node <--
    ... and please remember to wrap code with forum BBCode tags:-

    [CODE]...[/CODE] [HTML]...[/HTML] [PHP]...[/PHP]

    If you can't think outside the box, you will be trapped forever with no escape...

  3. #3
    Join Date
    Mar 2011
    Posts
    122
    You're also overwriting window.confirm.
    Code:
    confirm = confirm('[Blackjack] Click OK to generate your first card!');

  4. #4
    Join Date
    Oct 2017
    Posts
    1
    Yea having confirm as both variable and function is probably mucking things up.

    I would skip the variable and just do something like
    if (confirm('Are you ready to start?'))

    also using confirm to tell the user something would be better done with an alert I would say

    You planted the idea - now I want to create objects of cards and decks where the deck contains an array of cards that can be shuffled and each card has a number/face, value and a suit. Do the same thing a different way lol

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles