So the problem I'm tackling involves getting a dynamic range and splitting it into 6 parts.
For example a range can start from 6.1 and end at 93.6. Or as simple as 6-24.
At the end, what i am trying to do is to just get 6 different points in that range. If it's 1-24, that would be 4,8,12,16,20,24.
Here is an approach that I am using with not quite accurate results:
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);
So the problem I'm tackling involves getting a dynamic range and splitting it into 6 parts.
For example a range can start from 6.1 and end at 93.6. Or as simple as 6-24.
At the end, what i am trying to do is to just get 6 different points in that range. If it's 1-24, that would be 4,8,12,16,20,24.
Here is an approach that I am using with not quite accurate results:
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);
Share
Improve this question
edited Jul 9, 2018 at 17:12
Chiko
asked Jul 9, 2018 at 16:59
ChikoChiko
2,3504 gold badges21 silver badges29 bronze badges
7
- 1 And what is the problem you're encountering with this? – devlin carnate Commented Jul 9, 2018 at 17:02
- @devlincarnate the problem of how to approach it, are you kind enough to have a solution I can use? – Chiko Commented Jul 9, 2018 at 17:04
- Have you tried dividing the number of possible numbers in the range by 6? So, in the case of 1-24, there are 24 numbers in the range. 24 / 6 = 4, which gives you your offset. Of course, you'll have to account for precision but you haven't specified what should happen when you have something other than a whole number that divides evenly by 6. – devlin carnate Commented Jul 9, 2018 at 17:06
-
Is your trouble to calculate iteration step?
var step = (maxVal - minVal)/6
? Or is it difficult to make 6 iterations to fill array? Just begin fromminValue
and then increase variable value bystep
. – Alexander Commented Jul 9, 2018 at 17:10 - please add a prehensible example. – Nina Scholz Commented Jul 9, 2018 at 17:12
2 Answers
Reset to default 10For six parts, you need five offsets. This offset is calculated by the delta of left and right divided by parts minus one.
Then iterate until all smaller parts are taken, at the end take the right value without any calculation to prevent floating point arithmetic errors.
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; }
Here you go with a solution
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);
Hope this will help you start the work.