JSFiddle - React, Tailwind, and code Playground
by Steven Senkus
JavaScript
const arrA = [55,12,1, 2, 3, 11, 13];
const arrB = [100,8, 23, 200, 247, 299];
function smallestDiffBetweenElements(arr1, arr2) {
let smallestDiffIndices = [];
let smallestDiff = 999999;
for (let i = 0; i < arrA.length; i++) {
const currItemA = arrA[i];
for (let j = 0; j < arrB.length; j++) {
const currItemB = arrB[j];
const currentDiff = Math.abs(currItemA - currItemB);
if (currentDiff < smallestDiff) {
smallestDiff = currentDiff;
smallestDiffIndices = [currItemA, currItemB];
}
}
}
return smallestDiffIndices;
}
console.log(smallestDiffBetweenElements(arrA, arrB));