Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

142
Views
How to run if conditional, can meet twice

I have a condition like this, I want the first if conditional to have no reaction if you meet this condition again the second time give him a reaction, more or less like the example in the code below.

var x = -1;

if( x < 0){
  console.log("is okay")
}if( x < 0 AGAIN ){
  console.log("is not okay")
}
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

add a new variable can solve it:

var x = -1;
var isFirstTime = true;

if( x < 0 && isFirstTime == true){
  console.log("is okay");
  isFirstTime = false;
}if( x < 0 && isFirstTime == false){
  console.log("is not okay");
}
about 4 years ago · Juan Pablo Isaza Report

0

Use a flag variable and initialize it with true, when it will meet the first negative value (x < 0) print ok and set the flag to false. Next time when it got a negative value and the flag is false print not ok.

var x = -1;
var firstTime = true;
if( x < 0){
    if (firstTime){
        console.log("is okay");
        firstTime = false;
   }
    else {
      console.log("is not okay")
  }
 }
 

Here is a test example:

var firstTime = true;
test_list = [1,-1,2,5,-1,6]

for (let i in test_list){
    x=test_list[i]
  console.log(x)
    if( x < 0){
    if (firstTime){
    console.log("is okay");
    firstTime = false;
  }
  else {
    console.log("is not okay")
  }
 }
}

Output:

1
-1
"is okay"
2
5
-1
"is not okay"
6
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!