0

I am trying to target a variable from a variable value that has been passed in from function arguments. I don't know how to do this. P.S. the variables at the top are also used by other functions

let cardvalue0 = false;
let cardvalue1 = false;

function myfunction (card) {
    if (card === false) {
        card = true;
        //ether cardValue 0 or 1 should now be true
        return card;
    }
}

// exturnal html
<button onclick="myfunction("cardValue0")"></button>
<button onclick="myfunction("cardValue1")"></button>

new try by me

//define cards
let card0State = false;
let card1State = false;


//toggle card status (read or not)
function cardToggle(card) {

  console.log(card);

  card = !card
  console.log(card);
  return card;
}
// external html
<button onclick="myfunction(cardValue0)"></button>
<button onclick="myfunction(cardValue1)"></button>
Félix Brunet
  • 1,993
  • 1
  • 18
  • 22
  • 1
    If using ES 6, you could use Map to store key values. Key will be cardvalue0/cardvalue1 and value can be true or false. – Sunny Jun 21 '18 at 16:28
  • 1
    the html `onclick` should only send back a string representation ... you'll probably need to add in a logic step to check the text sent back from the html, to match it with its appropriate variable in your javascript (or you can use `eval()` but 99% of devs would discourage that course) – Doug Jun 21 '18 at 16:41

3 Answers3

0

Would it be possible to store card0State and card1State in an object instead? Then you could reference them as keys to the object instead.

const cardvalues = {};
cardvalues.cardvalue0 = false;
cardvalues.cardvalue1 = false;

function myfunction (cardNumber) {
    if (cardvalues[cardNumber] === false) {
        cardvalues[cardNumber] = true;
        //ether cardValue 0 or 1 should now be true
        return cardvalues[cardNumber];
    }
}

// exturnal html
<button onclick="myfunction('cardvalue0')"></button>
<button onclick="myfunction('cardvalue1')"></button>

Then you aren't declaring a new variable in the function scope and you are directly altering the key values on the object. This is usually better than a variable variable pattern.

"Variable" variables in Javascript?

greendemiurge
  • 509
  • 3
  • 11
0

the simpler would be moving the original variable (cardvalue0 and cardvalue1) in a Map or an object, so you could have :

cards = {
  cardvalue0 : false,
  cardvalue1 : false
}

function foo(cardName) {
  cards[cardName] = !cards[cardName];
  return cards[cardName]
  }
}

if you can not move cardvalue0 and cardvalue1, you could simply hardcode them, but it will become ugly really fast.

function foo(cardName) {
  if(cardName === "cardvalue0") {
       cardValue0 = !cardValue0; 
       return cardValue0;
  } else if(cardName === "cardvalue1")
      cardValue1 = !cardValue1; 
      return !cardValue1;
}

eval would be the only way to get a variable from a string representation, but is not a good idea.

Félix Brunet
  • 1,993
  • 1
  • 18
  • 22
0

target a variable from a variable value? If I understood you right you want to access a variable cardvalue0 inside the function myfunction.

You can access it using, eval(card) but definitely not advised to do

function myfunction (card) {
  if (eval(card) === false) { //this will work
    card = true; //however you cannot change the original value

    return card;
  }
}

You can get this by using objects

let cardDict = { "cardvalue0" : false,
                 "cardvalue1" : false}

function myfunction (card) {
  if (cardDict[card] === false) { 
    cardDict[card] = true; 
    return cardDict[card];
  }
}
Anto
  • 610
  • 5
  • 13