0

I am trying to create a helper function that in part takes in a global variable for reassignment. Trying to avoid using eval to make this work.

let oneQuestionCount,
    twoQuestionCount,
    oneQuestionsChecked,
    twoQuestionsChecked;

function hideFadeIn(divToHide, divToShow, countItem = null, checkedItem = null) {
    $("div#item-" + divToHide).hide();
    $("div#item-" + divToShow).fadeIn();
    if (countItem) {
      countItem = $("div#item-" + divToShow + " :input").length; // want the global var to be changed not the function scope var
    }
    if (checkedItem) {
      checkedItem = $("div#item-" + divToHide + " :checked").length; // want the global var to be changed not the function scope var
    }

hideFadeIn(
      "one",
      "two",
      twoQuestionCount, // how to pass in and change globally?
      oneQuestionsChecked // how to pass in and change globally?
    );

console.log(twoQuestionCount, oneQuestionsChecked); // should be reassinged value by the function.

There will be multiple function calls and other global variables that need to be assigned - hence the helper function. ex: hideFadeIn("one","two",twoQuestionCount, oneQuestionsChecked); then hideFadeIn("two","three",threeQuestionCount, twoQuestionsChecked); then hideFadeIn("three","four",threeQuestionCount, fourQuestionsChecked); etc...

5
  • 3
    So, given that they are global and you can change them without passing them in..... why? Commented Jun 5, 2019 at 14:10
  • Bc I need to pass in lots of questions, not just 1 or 2. Commented Jun 5, 2019 at 14:12
  • Noooo.... they're global. You understand what global means, yeah? Otherwise you need to explain what the problem is more clearly. Because as it is currently written, it sounds like you are trying to solve a self imposed problem. Commented Jun 5, 2019 at 14:13
  • Yes, I do, but I need to pass in the pointers for reassignment. As I'll have tons of calls to the function I need a way to key off of which GV to target. So, I can pass text then eval it maybe into the pointer of the GV or I was trying to pass the GV reference to the function. ex: hideFadeIn("one","two",twoQuestionCount, oneQuestionsChecked); then hideFadeIn("two","three",threeQuestionCount, twoQuestionsChecked); then hideFadeIn("three","four",threeQuestionCount, fourQuestionsChecked); etc... Commented Jun 5, 2019 at 14:16
  • Then change your variable structure to the way T.J. has in his answer so you avoid eval(). And if the whatever in his example is global, you still would not have to pass it in to the method. Commented Jun 5, 2019 at 14:19

1 Answer 1

2

You can't do that. When you pass twoQuestionCount into hideFadeIn, its value is passed, not the variable.

If you like, you could put those in an object, and then pass in the object:

let whatever = {
    oneQuestionCount: 0,
    twoQuestionCount: 0,
    oneQuestionsChecked: 0,
    twoQuestionsChecked: 0
};

then

hideFadeIn("one", "two", whatever);

hideFadeIn can change the properties on the object it receives.

Sign up to request clarification or add additional context in comments.

Comments

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.