0

is it possible to do a checker for this scenario

  • An infinite loop of number, example 1 2 3 4 is coming into the server.
  • However I only want to accept these number once.

Here is what I have done so far. I think i am lacking of one more conditional check.

var check = false;

if(check == false)
{
     check = true; // But once check is true the number will never come in
     //For example, this will print 1. but it will not print 2 3 4.
     //If i do not have the checker, it will print 1 2 3 4 1 2 3 4 1 2.. infinity time.


}

I have done an additional checker using loops to fix this issue

2
  • 2
    With which values you are applying a check ? May be you should deal with array Commented Apr 8, 2016 at 6:15
  • can you post the complete code Commented Apr 8, 2016 at 6:15

2 Answers 2

1

Do you know about the number of integers coming in a single loop? if yes, you can try something like this...

var counter=0;//outside the loop
var check = false;
var n=4;//no. of integers in single loop

if(check == false)
{  
     counter++;
     if(n==counter){
       check = true; 
     }

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

Comments

1
var logOnce = (function() {
  var seen = {};
  return function(value) {
    if (!seen[value]) {
      seen[value] = true;
      console.log(value);
    }
  };
})();

logOnce(1);
// => 1
logOnce(2);
// => 2
logOnce(1);
logOnce(3);
// => 3
logOnce(3);
logOnce(3);

2 Comments

An infinite loop of number?
@RIYAJKHAN: Without a clarification, it's impossible to know what is meant by it. But the approach here is general enough.

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.