Comments

Coordinator proactive3 months ago

A single loop-through solution with a simple helper function for improved readability. In principle, the very same approach as Abdelhamid's, just structured in a slightly different manner:

const solve = (intArray, p) => {
  const getDistance = (x, y) => {
    return Math.sqrt(x ** 2 + y ** 2);
  }
 
  let closestDistance = 0, closestReferencePoint = 0;
  intArray.forEach((r, i) => {
    const distance = getDistance(Math.abs(r[0] - p[0]), Math.abs(r[1] - p[1]));

    if (!closestDistance || closestDistance > distance ) {
      closestDistance = distance;
      closestReferencePoint = i;
    }
  });
  return intArray[closestReferencePoint];
}

Coordinator proactive3 months ago

A condensed, a bit simplified approach just crossed my mind:

const solve = (intArray, p) => {
  let closestDistance = 0, closestReferencePoint = 0;
  intArray.forEach((r, i) => {
    const distance = Math.sqrt((r[0] - p[0]) ** 2 + (r[1] - p[1]) ** 2);

    if (!closestDistance || closestDistance > distance ) {
      closestDistance = distance;
      closestReferencePoint = i;
    }
  });
  return intArray[closestReferencePoint];
}

Ameliorated Tugrik3 months ago

Wow, guys! I've almost written the complete application! It was looooong one! Who said the level task is EASY? To my mind it's at least MEDIUM.

const solve = (mainArr, p) => {
  let closest = Infinity;
  
  const target = p[0] + p[1];
  let tempArr = mainArr.map(i => i[0] + i[1])
  
  /*One step at a time*/
  tempArr.push(target);
  tempArr = Object.entries(tempArr)
  tempArr.sort(([, a], [, b]) => a - b);
  const index = tempArr.findIndex(i => +i[0] === mainArr.length)
  
  /*Watch out the edge on the left side!*/
  if (index === 0) {  
    closest = +tempArr[index + 1][0]
    return mainArr[closest];
  }
  
  /*Now, be careful on the right side! The path is slippery.*/
  if (index === tempArr.length - 1) { //closest left, at the edge;
    closest = +tempArr[index - 1][0];
    return mainArr[closest];
  }
  /*Hm... Let me guess... Are they equal? Them seem to be rather equal.*/
  if (tempArr[index - 1][1] === target || tempArr[index + 1][1] === target)
    return p;
  /*Let's weight two values on both sides which one is the closest.*/
  if ((tempArr[index - 1][1] - target) * -1 < tempArr[index + 1][1] - target) {
    closest = +tempArr[index - 1][0]
    return mainArr[closest];
  } else {
    /*He-he...*/
    closest = +tempArr[index + 1][0];
    return mainArr[closest];
  }
}

Forges4 months ago

const solve = (intArray, p) => {
  const arr = [...intArray];
  const index = arr
    .map(item => [p[0] - item[0], p[1] - item[1]])
    .map((item, index) => [Math.abs(item[0] + item[1]), index])
    .sort((a, b) => a[0] - b[0])[0][1];

  console.log(index);

  return intArray[index];
};

Ameliorated Tugrik3 months ago

Haha! Mate. I'm looking at your four-lines code and smiling. It's so obvious. You're rock! :)

redundant Districta year ago

const solve = (intArray, p) => { var myNewArray4 = [].concat(...intArray); var arr1 = []; var arr2 = [] for(var i = 0; i < myNewArray4.length; i += 2){ arr1.push(Math.abs(myNewArray4[i] - p[0])); } for(var i = 0; i < myNewArray4.length; i += 2){ arr2.push(Math.abs(myNewArray4[i] - p[1])); } var comp = [] for(var i = 0; i < arr1.length; i++){ comp.push(arr1[i] + arr2[i]) } const min = Math.min(...comp) return intArray[comp.indexOf(min)] };

Abdelhamid Ismaila year ago

That Was Tough

const solve = (intArray, p) => {
  let thePoint = [];
  let currentDistance = Math.sqrt(
    Math.pow(intArray[0][0] - p[0], 2) + Math.pow(intArray[0][1] - p[1], 2)
  );
  intArray.forEach((point) => {
    if (
      Math.sqrt(Math.pow(point[0] - p[0], 2) + Math.pow(point[1] - p[1], 2)) <
      currentDistance
    ) {
      currentDistance = Math.sqrt(
        Math.pow(point[0] - p[0], 2) + Math.pow(point[1] - p[1], 2)
      );
      thePoint = point;
    }
  });
  return thePoint;
};

zunair jarrala year ago

below works fine in line with Math.sqrt () multiplication helps to deal with negative integers other without this math.sqrt the negative number becomes nearest point that is not right

const solve = (intArray, p) => { let minDistance = 10000000; let closestPoint; for (let i = 0; i < intArray.length; i++) { distance = Math.sqrt((p[0] - intArray[i][0]) * (p[0] - intArray[i][0]) + (p[1] - intArray[i][1]) * (p[1] - intArray[i][1])); if (distance < minDistance) { minDistance = distance; closestPoint = intArray[i]; }
} return closestPoint; };