0

This part of a question I asked on here last week. It was answered, except now I'm going to use this function multiple times, and instead of repeating the function multiple times I want to use arguments/parameters, but there are issues....

html:

<button id="button1">a</button>
<button id="button2">b</button>

javascript:

var funcAPressed = false;

var funcA = function(x, y) {
  alert(x);
  y = true;
  alert(y);
};
var funcB = function(x, y) {
  if (funcAPressed === false){
    alert(x);
  }
  else{
    alert(y);
  }
};

var buttonA = document.getElementById('button1');
buttonA.addEventListener('click', function(){funcA("Button A has been pressed", funcAPressed);});

var buttonB = document.getElementById('button2');
buttonB.addEventListener('click', function(){funcB("press Button A", "Button B has been pressed");});

Here's how this is all set up: I want the user to press the buttons in the correct order, A then B. if B is pressed first, there's an alert message. The answer I got from my previous question on here was to create a boolean and set it to false, which I did at the top. When I click the button, I get the alert message below, and it looks like funcAPressed becomes true since I did the alert(y) which alerts "true."

The problem is that when I click button B after clicking Button A, it still sees funcAPressed = false instead of it being true.

The funcB will work fine if instead of y = true I have funcAPressed = true...but by doing that I'll have to copy this function multiple times in my document instead of only being able to use it once and assign parameters.

  • you thought that funcAPressed was passed by reference, and when you set y to true, you assumed that funcAPressed was set to true right? Read this answer: http://stackoverflow.com/a/13104500/916000 – Taha Paksu Apr 24 '17 at 06:33

2 Answers2

0

Kindly understand funcAPressed != y inside the funA .you are only passing variable value,not the variable. you are replace the value of y not with funcAPressed varible .so directly replace the variable value using funcAPressed = true

var funcAPressed = false;

var funcA = function(x, y) {
console.log(x)
  funcAPressed = true;
  console.log();
};
var funcB = function(x, y) {
  if (funcAPressed === false) {
    console.log(x);
  } else {
    console.log(y);
  }
};

var buttonA = document.getElementById('button1');
buttonA.addEventListener('click', function() {
  funcA("Button A has been pressed", funcAPressed);
});

var buttonB = document.getElementById('button2');
buttonB.addEventListener('click', function() {
  funcB("press Button A", "Button B has been pressed");
});
<button id="button1">a</button>
<button id="button2">b</button>
prasanth
  • 22,145
  • 4
  • 29
  • 53
  • I get what you're saying, but the reason why I won't do funcAPressed = true within funcA is because I'm going to run this kind of function multiple times on the page. If funcAPressed is declared true in only one instance, it will be true in every instance after that. I need this kind of check to happen more than once. –  Apr 24 '17 at 06:20
0

Try this.

var funcAPressed = false;

var funcA = function(x, y) {
  if (funcAPressed === false && y== "btnB"){
    console.log("please press button a");
  }else{
    funcAPressed = true;
    console.log(x);
  }
};

var buttonA = document.getElementById('button1');
buttonA.addEventListener('click', function(){funcA("Button A has been pressed", "btnA");});

var buttonB = document.getElementById('button2');
buttonB.addEventListener('click', function(){funcA("Button B has been pressed", "btnB");});
<button id="button1">a</button>
<button id="button2">b</button>
Krupesh Kotecha
  • 2,396
  • 3
  • 21
  • 40