最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Split a dynamic range into equal parts using js - Stack Overflow

programmeradmin1浏览0评论

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 from minValue and then increase variable value by step. – Alexander Commented Jul 9, 2018 at 17:10
  • please add a prehensible example. – Nina Scholz Commented Jul 9, 2018 at 17:12
 |  Show 2 more ments

2 Answers 2

Reset to default 10

For 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.

发布评论

评论列表(0)

  1. 暂无评论
ok 不同模板 switch ($forum['model']) { /*case '0': include _include(APP_PATH . 'view/htm/read.htm'); break;*/ default: include _include(theme_load('read', $fid)); break; } } break; case '10': // 主题外链 / thread external link http_location(htmlspecialchars_decode(trim($thread['description']))); break; case '11': // 单页 / single page $attachlist = array(); $imagelist = array(); $thread['filelist'] = array(); $threadlist = NULL; $thread['files'] > 0 and list($attachlist, $imagelist, $thread['filelist']) = well_attach_find_by_tid($tid); $data = data_read_cache($tid); empty($data) and message(-1, lang('data_malformation')); $tidlist = $forum['threads'] ? page_find_by_fid($fid, $page, $pagesize) : NULL; if ($tidlist) { $tidarr = arrlist_values($tidlist, 'tid'); $threadlist = well_thread_find($tidarr, $pagesize); // 按之前tidlist排序 $threadlist = array2_sort_key($threadlist, $tidlist, 'tid'); } $allowpost = forum_access_user($fid, $gid, 'allowpost'); $allowupdate = forum_access_mod($fid, $gid, 'allowupdate'); $allowdelete = forum_access_mod($fid, $gid, 'allowdelete'); $access = array('allowpost' => $allowpost, 'allowupdate' => $allowupdate, 'allowdelete' => $allowdelete); $header['title'] = $thread['subject']; $header['mobile_link'] = $thread['url']; $header['keywords'] = $thread['keyword'] ? $thread['keyword'] : $thread['subject']; $header['description'] = $thread['description'] ? $thread['description'] : $thread['brief']; $_SESSION['fid'] = $fid; if ($ajax) { empty($conf['api_on']) and message(0, lang('closed')); $apilist['header'] = $header; $apilist['extra'] = $extra; $apilist['access'] = $access; $apilist['thread'] = well_thread_safe_info($thread); $apilist['thread_data'] = $data; $apilist['forum'] = $forum; $apilist['imagelist'] = $imagelist; $apilist['filelist'] = $thread['filelist']; $apilist['threadlist'] = $threadlist; message(0, $apilist); } else { include _include(theme_load('single_page', $fid)); } break; default: message(-1, lang('data_malformation')); break; } ?>