NicolaL1995
NicolaL1995

Reputation: 17

Handle return statement in a better way

That's my function:

 const checkifHaveBomb = (column, row) => {
            let isBomb = false
            activeBombContainer.forEach(element => {

                if (element.column === column && element.row === row) {
                    isBomb = true;
                    return;
                }
            })
            if (isBomb) return true;
            else
                return false;
        }

This was my 1st try:

 const checkifHaveBomb = (column, row) => {

            activeBombContainer.forEach(element => {
                if (element.column === column && element.row === row) {
                   
                    return true;
                }
            })
            
            return false;
        }

I was thinking that returns will end the function, but i didnt work. I suppose that the return statement is applied to forEach instead of checkifHaveBomb .

There is a better way to write this?

Upvotes: 0

Views: 49

Answers (3)

Stefan Pfaffel
Stefan Pfaffel

Reputation: 129

You want to iterate through the activeBombContainer array until the condition returns true. Sounds like find function might suit your needs:

The find() method returns the first element in the provided array that satisfies the provided testing function. If no values satisfy the testing function, undefined is returned.

You should be able to write the function as follows

checkifHaveBomb = (column, row) => {
    return activeBombContainer.find(element => {
        return element.column === column && element.row === row
    })
}

Alternatively you could use the some function because it returns a boolean value.

checkifHaveBomb = (column, row) => {
    return activeBombContainer.some(element => {
        return element.column === column && element.row === row
    })
}

Upvotes: 0

traktor
traktor

Reputation: 19301

You could use Array.prototype.some instead of forEach.

const checkifHaveBomb = (column, row)
=> activeBombContainer.some( element=>element.column === column && element.row === row);

As you surmised, the return value of Array.prototype/forEach's argument function is discarded after each call.

Upvotes: 2

Konrad
Konrad

Reputation: 24661

Problem

return ends the function, forEach just calls a function for each element

Solutions

Some

You can try using some and return true if you want to finish:

const checkifHaveBomb = (column, row) => {
  let isBomb = false
  activeBombContainer.some(element => {

    if (element.column === column && element.row === row) {
      isBomb = true;
      return true;
    }
  })
  if (isBomb) return true;
  else return false;
}

Or even shorter:

const checkifHaveBomb = (column, row) => {
  return activeBombContainer.some(element => {
    if (element.column === column && element.row === row) {
      isBomb = true;
      return true;
    }
  })
}

loop statement

You can also just use a normal for..of loop

const checkifHaveBomb = (column, row) => {
  for (const element of activeBombContainer) {
    if (element.column === column && element.row === row) {
      return true
    }
  }
  return false
}

Upvotes: 2

Related Questions