1

The code I wrote below works, but the variables I named within the each function are not defined after the function completes.

jQuery.each($myObject, function(i, val) {
  ce = $(this).attr('class');
  if (i === 0) {
    step1complete = true;
    if (/incomplete/i.test(ce)) {
      var step1complete = false
    }
    console.log('step1' + step1complete);
  } else if (i === 1) {
    step2complete = true
    if (/incomplete/i.test(ce)) {
      var step2complete = false
    }
    console.log('step2' + step2complete);
  } else if (i === 2) {
    step3complete = true
    if (/incomplete/i.test(ce)) {
      var step3complete = false
    }
    console.log('step3' + step3complete);
  }
});

if (step1complete === true && step2complete == false && step3 == false) {
  console.log('Youre Just Starting Out I see');
} else {
  console.log('You got one step done now theres more!');
}

However, its saying the variables I set in the each function are not defined even though I see them in the console during the loop.

4
  • share the entire code. Commented Apr 7, 2018 at 23:19
  • 1
    @DamienGold If you understand JavaScript, the above code is enough. :) Commented Apr 7, 2018 at 23:21
  • @Praveen Kumar I do know javascript. Commented Apr 7, 2018 at 23:23
  • @DamienGold Oh sorry. But I was able to find the issue with his code, the moment I saw it. That seems to be the full code. What exactly were you expecting to see? :) Just curious to know. Commented Apr 7, 2018 at 23:24

1 Answer 1

1

Don't use var as it will scope it inside the function that is enclosed. Instead, bring them out, just the var part.

var step1complete;
var step2complete;
var step3complete;
jQuery.each($myObject, function(i, val) {
  ce = $(this).attr('class');
  if (i === 0) {
    step1complete = true;
    if (/incomplete/i.test(ce)) {
      step1complete = false
    }
    console.log('step1' + step1complete);
  } else if (i === 1) {
    step2complete = true
    if (/incomplete/i.test(ce)) {
      step2complete = false
    }
    console.log('step2' + step2complete);
  } else if (i === 2) {
    step3complete = true
    if (/incomplete/i.test(ce)) {
      step3complete = false
    }
    console.log('step3' + step3complete);
  }
});

if (step1complete === true && step2complete == false && step3 == false) {
  console.log('Youre Just Starting Out I see');
} else {
  console.log('You got one step done now theres more!');
}

If you don't want to pollute the global scope, use an IIFE:

(function () {
  var step1complete;
  var step2complete;
  var step3complete;
  jQuery.each($myObject, function(i, val) {
    ce = $(this).attr('class');
    if (i === 0) {
      step1complete = true;
      if (/incomplete/i.test(ce)) {
        step1complete = false
      }
      console.log('step1' + step1complete);
    } else if (i === 1) {
      step2complete = true
      if (/incomplete/i.test(ce)) {
        step2complete = false
      }
      console.log('step2' + step2complete);
    } else if (i === 2) {
      step3complete = true
      if (/incomplete/i.test(ce)) {
        step3complete = false
      }
      console.log('step3' + step3complete);
    }
  });

  if (step1complete === true && step2complete == false && step3 == false) {
    console.log('Youre Just Starting Out I see');
  } else {
    console.log('You got one step done now theres more!');
  }
})();

Definition:

IIFE (Immediately Invoked Function Expression) is a JavaScript function that runs as soon as it is defined. It is a design pattern which is also known as Self-Executing Anonymous Function and contains two major parts. The first is the anonymous function with lexical scope enclosed within the Grouping Operator (). This prevents accessing variables within the IIFE idiom as well as polluting the global scope.

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

11 Comments

What does use a closure mean? nvm i see now thanks for example!
@weekapaug I have updated it to IIFE. Check out the link.
@weekapaug IIFE (Immediately Invoked Function Expression) is a JavaScript function that runs as soon as it is defined. It is a design pattern which is also known as Self-Executing Anonymous Function and contains two major parts. The first is the anonymous function with lexical scope enclosed within the Grouping Operator (). This prevents accessing variables within the IIFE idiom as well as polluting the global scope.
Great links & examples! cant wait to dig in, thanks!
A quick newbie question on this. is var mynewvar the same as saying mynewvar=true? even though its not set to anything specific?
|

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.