Untitled

Run Settings
LanguageJavaScript
Language Version
Run Command
// cabs = [1, 0, 0], [2, 5, 10], [3, 5, 1000], [4, 1000, 2], [5, 3, 4], [6, -4, -5], [7, -2, 3] // userLocation = [0,0] // r = 5 const cabs = [[1, 0, 0], [2, 5, 10], [3, 5, 1000], [4, 1000, 2], [5, 3, 4], [6, -4, -5], [7, -2, 3]]; let userLocation = [0,0] let r = 5 let cab_user_dist = []; const findNearbyCabs = (cabs, userLocation, r) =>{ const distance = (x1, y1, x2, y2) => Math.hypot(x2 - x1, y2 - y1); cabs.map((cab)=>{ let [cabIdx,cabX,cabY] = cab; let dist =distance(cabX,userLocation[0],cabY,userLocation[1]); //if(dist <= r) { cab_user_dist.push([cabIdx,dist]); //} }); cab_user_dist.sort(function(a, b) { return a[1] - b[1]; }); console.log("Cab - Distance",cab_user_dist); return cab_user_dist; } let cab_user_dist_new = []; const updateCabLocation = (cabs, userLocation) =>{ let cab_user_dist_new = []; const distance = (x1, y1, x2, y2) => Math.hypot(x2 - x1, y2 - y1); let [cabIdx,cabX,cabY] = cabs; let dist =distance(cabX,userLocation[0],cabY,userLocation[1]); cab_user_dist_new.push([cabIdx,dist]); console.log("New Distance Cab-distance",cab_user_dist_new); let updatedDist = cab_user_dist.map((el,i)=>{ if(el[0] == cab_user_dist_new[0][0]){ //cab_user_dist.splice(i,1); console.log("SPLICED",cab_user_dist,i) cab_user_dist.splice(i,1, [el[0],cab_user_dist_new[0][1]]); console.log("SPLICED",cab_user_dist,i) return [el[0],cab_user_dist_new[0][1]]; }else{ return el; } }); console.log("Cab-Distance Updated",updatedDist); //return cab_user_dist_new; } findNearbyCabs(cabs,userLocation,r); updateCabLocation([3,5,4000], [1,1])
Editor Settings
Theme
Key bindings
Full width
Lines