0

I need to add +1 to each card clicked but

Function result 3 as undefined why? what is wrong with my code?

card2 and card3 function recive each variable inside each click function

inside function click2 setcard(card2); click3 setcard(card3); and so on

For some reason it do not work.

var card2 = 0;
var card3 = 0;

function setcard(card2, card3) {
  document.getElementById("truecount").innerHTML = card2 + card3;
}

//functions

function clickplus2() {
  resultado += 2;
  zen += 2;
  cartas += 1;
  valor += 2;
  zenok += 11;

  card2 += 1;
  setresultado(resultado, zen, cartas, valor, zenok);
  setcard(card2);
}
function clickplus3() {
  resultado += 3;
  zen += 2;
  cartas += 1;
  valor += 3;
  zenok += 10;

  card3 += 1;
  setresultado(resultado, zen, cartas, valor, zenok);
  setcard(card3);
}
6
  • 5
    The parameters card2 and card3 in setcard shadow the global variables, so if you don't pass a second argument to setcard() when you call it, it will always be undefined Commented Sep 2, 2024 at 23:19
  • setcard(card2, card3) should be like this? Commented Sep 2, 2024 at 23:31
  • @OtávioBarreto Yes, either do setcard(card2, card3) or remove the parameters from setcard(), so it uses the global variables. Commented Sep 2, 2024 at 23:34
  • Just a suggestion: It's 2024 now. 1. Don't use implicit global variables in your function. 2. use let or const instead of var to declare variables. Commented Sep 3, 2024 at 0:20
  • 1
    also, you say the function returns undefined ... since you don't return any value in any of your fuctions, this is as expected Commented Sep 3, 2024 at 4:25

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.