如何使用 JavaScript 生成长度为 x 的线段上 n 个等距点的数组?
javascriptweb developmentobject oriented programming更新于 2025/11/6 6:36:17
要生成长度为 x 的线段上 n 个等距点的数组,请使用以下语法 −
for (var anyVariableName= 0; yourVariableName< yourStartPointName;
yourVariableName++) {
var v = (yourVariableName+1)/ (yourStartPointName+1);
var v2 = v*yourEndPointName;
示例
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.
Here, my file name is demo53.js
输出
这将产生以下输出 −
PS C:\Users\Amit\JavaScript-code> node demo54.js The Points= [ 8, 17, 25, 33, 42 ]
相关文章
有用资源
javascript 参考教程 - 该教程包含有关 javascript 的更多信息:https://www.cainiaomax.com/javascript/

