因此,我要解决的问题涉及获得动态范围并将其分为6部分。
例如,范围可以从6.1开始到93.6结束。或只需6-24。
最后,我想做的就是在该范围内获得6个不同的点。如果是1-24,那就是4,8,12,16,20,24。
这是我使用的一种方法,其结果不太准确:
const range = {start: '', end: ''};
const offset = Math.ceil(range.end / 6);
const markers = []
for(let i = offset; i <= range.end; i++){
if(range.end % i == 0){
markers.push(i)
}
}
console.log(markers);
答案 0 :(得分:3)
对于六个零件,您需要五个偏移量。该偏移量是通过左右差值除以零件减去一而得出的。
然后进行迭代,直到取到所有较小的部分为止,最后在不进行任何计算的情况下获取正确的值,以防止浮点运算错误。
function split(left, right, parts) {
var result = [],
delta = (right - left) / (parts - 1);
while (left < right) {
result.push(left);
left += delta;
}
result.push(right);
return result;
}
console.log(split(6, 24, 6));
console.log(split(6.1, 93.6, 6));
.as-console-wrapper { max-height: 100% !important; top: 0; }
答案 1 :(得分:1)
您在这里找到解决方法
var start = parseFloat(Math.random() * 10);
var end = parseFloat(Math.random() * 100);
var diff = ((end - start) / 6);
console.log("Start: ", start)
for(var i=0; i<6; i++){
start += parseFloat(diff);
console.log(start);
}
console.log("End: ", end);
希望这会帮助您开始工作。