Javascript function to get cellId from map point I wrote this, and I would like to know if this code is clean.

It lets me know if a point is in the middle of an isometric cell.

const getMapPointFromCellId = cellId =&gt; {

    var {x, y} = getCoordinatesFromCellId(cellId)

    x = x * constants.cellWidth
    if (y % 2) x += constants.offsetWidth

    y = y * constants.offsetHeight 


    return {x, y}
}
const getCellIdFromMapPoint = (x, y) =&gt; {
   var cellId = 0
   while(cellId &lt; constants.numberCells){

       const cellCoordinates = getMapPointFromCellId(cellId)

       const offsetX = cellCoordinates.x + constants.offsetWidth
       const offsetY = cellCoordinates.y + constants.offsetHeight

       const endX = cellCoordinates.x + constants.cellWidth
       const endY = cellCoordinates.y + constants.cellHeight


       if (cellCoordinates.x &lt; x &amp;&amp; x &lt; cellCoordinates.x + constants.cellWidth)
          if (cellCoordinates.y &lt; y &amp;&amp; y &lt; cellCoordinates.y + constants.cellHeight){

            if (x &lt; offsetX &amp;&amp; y &lt; offsetY) 
                if ((offsetY - y) / (cellCoordinates.x - x) &gt; (offsetY - cellCoordinates.y) / (cellCoordinates.x - offsetX)) return cellId
            if (x &gt; offsetX &amp;&amp; y &lt; offsetY)
                if ((offsetY - y) / (endX - x) &lt;= (offsetY - cellCoordinates.y) / (endX - offsetX)) return cellId
            if (x &lt; offsetX &amp;&amp; y &gt; offsetY) 
                if ((offsetY - y) / (cellCoordinates.x - x) &lt;= (offsetY - endY) / (cellCoordinates.x - offsetX)) return cellId
            if (x &gt; offsetX &amp;&amp; y &gt; offsetY) 
                if((offsetY - y) / (endX - x) &gt;= (offsetY - (constants.cellHeight - constants.offsetHeight)) / (endX - offsetX)) return cellId

        }

       cellId++
   }

  return -1
}
I always like to document my functions using high-level doc-blocks, e.g.

/**
 * Return the ID of the cell whose center is at the given point or -1 if ...
 *
 * @param {number} x - The x coordinate of the given point.
 * @param {number} y - The y coordinate of the given point.
 * @return {number} cell ID or -1 if no cell found at given point.
 */
const getCellIdFromMapPoint = (x, y) =&gt; { ... }


Also, I use inline comments to help the reader and my future self to understand complex statements such as

// Do C if A and B or C:
if (x &lt; offsetX &amp;&amp; y &lt; offsetY) 
   if ((offsetY - y) / (cellCoordinates.x - x) &gt; (offsetY - cellCoordinates.y) / (cellCoordinates.x - offsetX)) return cellId


Better yet, take those complex statements as a hint to better refactor your code.

I advise to use available language constructs where appropriate. If you replace a simple for-loop with a while-loop for no apparent reason, a fellow developer will have a harder time to read and understand your code.

Also, I really recommend to use curly brackets with if-statements. The lack of those might be more pleasing to the eye by hiding the deep nesting, but makes reading and understanding as well as modifying your code much harder.

I suggest to combine if-clauses with identical statements. Instead of writing e.g. 

if (A)
  if (B)
   return cellId;
if (C)
  if (D)
   return cellId


write e.g.

if ((A &amp;&amp; B) || (C &amp;&amp; D)) {
  return cellId;
}


Better yet, write helper functions to reduce the complex conditions, e.g.

if (isSomeCondition(A, B) || isSomeOtherCondition(C, D)) {
  return cellId;
}


Give those functions clear, self-documenting names to help the reader along. In your case, those could be e.g. isWithinRect(x, y, rect) or similar.

Your algorithm's runtime complexity is probably linear in the number of available cells. If you find that your application is running slow because of that, you can employ techniques such as spatial partitioning to improve runtime performance.