Switching Variable in Child Row with Const

Switching Variable in Child Row with Const

responsivelabsresponsivelabs Posts: 31Questions: 8Answers: 0
edited February 2020 in Free community support

So in one of my child rows, I've got a variable that will output a number. This number, I am trying to compare to data in a const and output the matching one in place of the original variable (in my case, round.number). Its stumping me a bit bc of how its within a child row of datatables. Here is an example showing the child rows:

http://live.datatables.net/zavoloqo/2/edit

And this is the const that I'm trying to use to compare/replace with round.number to const number as shown below:

// Index of 0 to 51 : ♦2 to ♣A ( ie. '4' equates to ♦3 )
    const CARDS = [ 
      ♦2, ♥2, ♠2, ♣2, ♦3, ♥3, ♠3, ♣3, ♦4, ♥4,  
      ♠4, ♣4, ♦5, ♥5, ♠5, ♣5, ♦6, ♥6, ♠6, ♣6, 
      ♦7, ♥7, ♠7, ♣7, ♦8, ♥8, ♠8, ♣8, ♦9, ♥9, 
      ♠9, ♣9, ♦10, ♥10, ♠10, ♣10, ♦J, ♥J, ♠J, 
      ♣J, ♦Q, ♥Q, ♠Q, ♣Q, ♦K, ♥K, ♠K, ♣K, ♦A, 
      ♥A, ♠A, ♣A 
    ]; 

// Game event translation ( output from round.number = CARDS[Math.floor(float * 52)] )
    const number = CARDS[Math.floor(float * 52)];

This question has an accepted answers - jump to answer

Answers

  • kthorngrenkthorngren Posts: 21,713Questions: 26Answers: 5,026
    edited February 2020

    If I understand your question correctly you can access the elements in the CARDS array with CARDS[ round.number ]. Just make sure CARDS is a global variable or within the scope of the format() function.

    Kevin

  • responsivelabsresponsivelabs Posts: 31Questions: 8Answers: 0
    edited February 2020

    It seems like any time I try to add the CARDS array anywhere it breaks datatables. Any ideas why this might be? Sorry the end const number = CARDS[Math.floor(float * 52)]; doesn't apply to this question.

    Essentially in my example, the number is 34 so I'm trying to display ♠10 as its 34 within the index.

  • kthorngrenkthorngren Posts: 21,713Questions: 26Answers: 5,026
    edited February 2020 Answer ✓

    The problem is more likely CARDS is an array of strings but you don't have quotes around them. Should look more like this:

    const CARDS = [
          "♦2", "♥2",.....
    

    You are probably getting a syntax error in your browser's console.

    Kevin

  • responsivelabsresponsivelabs Posts: 31Questions: 8Answers: 0

    That fixed it - dunno how I missed that, lol. Thanks for the second set of eyes!

  • responsivelabsresponsivelabs Posts: 31Questions: 8Answers: 0

    Oh one other thing - when I use CARDS[round.number], it will output the proper card if there is just a single number for round number but when it outputs an array (ie. 37, 19, 3), it just shows "undefined". How would I go about displaying them for the strings?

  • responsivelabsresponsivelabs Posts: 31Questions: 8Answers: 0
    edited February 2020

    Actually I figured this out on my own. Ended up using the following:

      function displayCardSymbol (number) {
        if(Array.isArray(number)){
          return  number.map(function(n){return CARDS[n];})
        } else {
          return CARDS[number];
        }
      }
    
  • kthorngrenkthorngren Posts: 21,713Questions: 26Answers: 5,026

    Perfect!

    Kevin

This discussion has been closed.