JSFiddle - React, Tailwind, and code Playground

by Kyle Lam

HTML

<h1 id="result"></h1>

JavaScript

var inputpoints=[{x:1,y:2},{x:2,y:2},{x:3,y:3},{x:1,y:1}];


function distanceOf(points){
    
    if (points.length<2){
        return 0;
    }
    
    if (points.length==2){
        return  Math.sqrt(Math.pow(points[0].x-points[1].x, 2)+Math.pow(points[0].y-points[1].y, 2))
    }

    
    total=0;
    for (i=0; i < points.length-1; i++){
        total+=distanceOf([points[i],points[i+1]]);
    }

    return total;
}
             


document.getElementById("result").innerHTML = distanceOf(inputpoints);