สร้างอาเรย์ของจุดที่กระจายตัวห่างกันเท่าๆ กัน (Equidistant) บนเส้นตรงที่มีความยาวกำหนด
ตัวอย่างโค้ด
function drawPoints(start, end) {
const arrayOfPoints = [];
for (var index = 0; index < start; index++) {
var v = (index + 1) / (start + 1);
var v2 = v * end;
arrayOfPoints.push(Math.round(v2));
}
return arrayOfPoints;
}
const arrayOfPoints = drawPoints(5, 50);
console.log("The Points=");
console.log(arrayOfPoints);
วิธีรันโปรแกรม
ใช้คำสั่ง node fileName.js (เช่น node demo53.js)
ผลลัพธ์
The Points= [ 8, 17, 25, 33, 42 ]