3 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 - p), Math.abs(r - p));

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

3 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 - p) ** 2 + (r - p) ** 2);

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

3 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 + p;
let tempArr = mainArr.map(i => i + i)

/*One step at a time*/
tempArr.push(target);
tempArr = Object.entries(tempArr)
tempArr.sort(([, a], [, b]) => a - b);
const index = tempArr.findIndex(i => +i === mainArr.length)

/*Watch out the edge on the left side!*/
if (index === 0) {
closest = +tempArr[index + 1]
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];
return mainArr[closest];
}
/*Hm... Let me guess... Are they equal? Them seem to be rather equal.*/
if (tempArr[index - 1] === target || tempArr[index + 1] === target)
return p;
/*Let's weight two values on both sides which one is the closest.*/
if ((tempArr[index - 1] - target) * -1 < tempArr[index + 1] - target) {
closest = +tempArr[index - 1]
return mainArr[closest];
} else {
/*He-he...*/
closest = +tempArr[index + 1];
return mainArr[closest];
}
}
``````

4 months ago

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

console.log(index);

return intArray[index];
};

``````

3 months ago

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

a 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)); } for(var i = 0; i < myNewArray4.length; i += 2){ arr2.push(Math.abs(myNewArray4[i] - p)); } 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)] };`

a year ago

That Was Tough

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

``````

a 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 - intArray[i]) * (p - intArray[i]) + (p - intArray[i]) * (p - intArray[i])); if (distance < minDistance) { minDistance = distance; closestPoint = intArray[i]; }
} return closestPoint; };