// (point) => coordinates var toString = … // Given point as array, // return coordinates as string. var toString = function (point) { return point.join(','); }; function toString(point) { return point.join(','); }
// (point) => coordinates var toString = (point) => point.join(','); // Given point as array, // return coordinates as string. var toString = function (point) { return point.join(','); }; function toString(point) { return point.join(','); }
sum of distances between its points: // between each point (after the first) and the preceding point. // A reducer function has up to 4 arguments: // (valuePrevious, element, index, array) => valueNext var lengthOfPolyline = (sum, point, index, points) => index === 0 ? sum // first point has no preceding point : sum + distanceBetweenPoints(point, points[index - 1]);
is square root of // sum of squares of differences between coordinates. var distanceBetweenPoints = (pointA, pointB) => Math.sqrt( (pointA[0] - pointB[0]) ** 2 + (pointA[1] - pointB[1]) ** 2); // ** is exponentiation operator in ECMAScript 2016
points is square root of // sum of squares of differences between coordinates. var distanceBetweenPoints = (pointA, pointB) => Math.sqrt( (pointA[0] - pointB[0]) ** 2 + (pointA[1] - pointB[1]) ** 2); // Rewrite function for any number of dimensions 1, 2, 3, … var distanceBetweenPoints = (pointA, pointB) => Math.sqrt( pointA.reduce((sum, coordA, index) => sum + (coordA - pointB[index]) ** 2, 0)); // (pointA[index] - pointB[index]) ** 2
// Put together a data structure // Take apart a data structure // Given 2D point as array, return coordinates as string. var toString = (point) => point[0] + ',' + point[1]; var toString = ([ x, y ]) => x + ',' + y; var pointsJ = pointArraysJ.map(([ x, y ]) => x + ',' + y).join(' ');
literal notation var point = [ 1 / 2, 29 / 63, ]; // Put together at left of = // with array index notation var point = []; point[0] = 1 / 2; point[1] = 29 / 63; // Take apart at left of = // with array destructuring var [ x, // 1 / 2 y, // 29 / 63 ] = point; // Take apart at right of = // with array index notation var x = point[0]; // 1 / 2 var y = point[1]; // 29 / 63
// Put together a data structure // Take apart a data structure // Given 2D point as object, return coordinates as string. var toString = (point) => point.x + ',' + point.y; var toString = ({ x, y }) => x + ',' + y; var pointsJ = pointObjectsJ.map(({ x, y }) => x + ',' + y).join(' ');
literal notation var point = { x: 1 / 2, y: 29 / 63, }; // Put together at left of = // with object dot notation var point = []; point.x = 1 / 2; point.y = 29 / 63; // Take apart at left of = // with object destructuring var { x, // 1 / 2 y, // 29 / 63 } = point; // Take apart at right of = // with object dot notation var x = point.x; // 1 / 2 var y = point.y; // 29 / 63
of points separated by space, // return array of coordinates as strings. var splitToStrings = (points) => points.split(' '); var pointsJ = '0.5,0.460 0.5,0.875 0.25,0.875'; var arrayOfStringsJ = splitToStrings(pointsJ); // [ '0.5,0.460', '0.5,0.875', '0.25,0.875' ]
coordinates as numbers, return point as object. var pointAsObject = (x, y) => ({ x: x, y: y }); // The shortcut requires arguments to have same names as properties. var pointAsObject = (x, y) => ({ x, y }); // When do you need parentheses? var f = (input) => { statements; }; // block var f = (input) => { statements; return something; }; // block var f = (input) => ({ properties }); // object instead of block
properties var x = 1 / 2; var y = 29 / 63; // Put together at right of = // with object literal shortcut var point = { x, y, }; // If variables // have same names as properties // Take apart at the left // with object destructuring var { x, y, } = point;