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

380
Views
Why do I have to IF return, IF what?

I am learning to code or rather - even reading the code at this moment. Could anyone please explain me, why those lines are there and what do they do?

  if (noClicking) return;
  if (e.target.classList.contains("flipped")) return;

What is the purpose of those two lines?

    function handleCardClick(e) {
  // you can use event.target to see which element was clicked

  if (noClicking) return;
  if (e.target.classList.contains("flipped")) return;

  let currentCard = e.target;
  currentCard.style.backgroundColor = currentCard.classList[0];

  if (!card1 || !card2) {
    currentCard.classList.add("flipped");
    card1 = card1 || currentCard;
    card2 = currentCard === card1 ? null : currentCard;
  }

  if (card1 && card2) {
    noClicking = true;
    // debugger
    let gif1 = card1.className;
    let gif2 = card2.className;

    if (gif1 === gif2) {
      cardsFlipped += 2;
      card1.removeEventListener("click", handleCardClick);
      card2.removeEventListener("click", handleCardClick);
      card1 = null;
      card2 = null;
      noClicking = false;
    } else {
      setTimeout(function() {
        card1.style.backgroundColor = "";
        card2.style.backgroundColor = "";
        card1.classList.remove("flipped");
        card2.classList.remove("flipped");
        card1 = null;
        card2 = null;
        noClicking = false;
      }, 1000);
    }
  }

  if (cardsFlipped === COLORS.length) alert("game over!");
}
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

if (noClicking) return;
if (e.target.classList.contains("flipped")) return;

The lines below those 2 lines will not be executed if one of the 2 lines are returned. It means if noClicking == true or e.target.classList.contains("flipped") == true, then all the lines below those 2 lines will not be executed.

about 4 years ago · Juan Pablo Isaza Report

0

While the answers already given are correct, I thought it could help to understand why those lines are there in place. They are called guard clauses. Their purpose is to make your code more efficient.

Think of a scenario like this:

function myFunc(someParam) {
  /*
  do something that takes a lot of time/resources

  ... then somewhere you find someParam was invalid!
  so all the calculations done until now was useless
  */
}

So instead of checking the validity of arguments, global variables, etc. when you need them in the function, you check for them in the beginning of the function such as like this:

function myFunc(someParam) {
  //let's say valid() is a function that checks for validity of the param,
  //returns true if valid, false if invalid.
  if(!valid(someParam)) return;
  /*
  do something that takes a lot of time/resources

  ... then your function will only run the expensive code
  if it is sure that there are no invalid arguments passed to it.
  */
}

It doesn't always have to be just return. This is your choice. Maybe the function always returns some positive number, then you could return -1 as such:

const getRoot = (num) => {
  if(num < 0) return -1;
  //get square root of the number
  return abs(Math.sqrt(num));
}

//Then when you call the function, you could check if the result is not -1,
//to make sure you passed the correct arguments.

if (getRoot(-1) < 0) 
  console.error("Uh oh, you passed a negative number to the square root function");

Or you could throw an Error:


function myFunc (someParam) {
  if(!valid(someParam)) throw new Error("wrong param bruh");

  //do something...
}
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!