Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 746965
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T14:09:04+00:00 2026-05-14T14:09:04+00:00

My javascript isnt so great, but i found a brilliant looking function here I’m

  • 0

My javascript isnt so great, but i found a brilliant looking function here

I’m not sure what to do with this bit:

var ranges = [], rstart, rend;

full function:

function getRanges(array) {
  var ranges = [], rstart, rend;
  for (var i = 0; i < array.length; i++) {
    rstart = array[i];
    rend = rstart;
    while (array[i + 1] - array[i] == 1) {
      rend = array[i + 1]; // increment the index if the numbers sequential
      i++;
    }
    ranges.push(rstart == rend ? rstart+'' : rstart + '-' + rend);
  }
  return ranges;
}

getRanges([2,3,4,5,10,18,19,20]);
// returns ["2-5", "10", "18-20"]
getRanges([1,2,3,5,7,9,10,11,12,14 ]);
// returns ["1-3", "5", "7", "9-12", "14"]
getRanges([1,2,3,4,5,6,7,8,9,10])
// returns ["1-10"]
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T14:09:04+00:00Added an answer on May 14, 2026 at 2:09 pm

    It’s almost exactly the same in PHP.

    <?php
    
    function getRanges($array){
        $ranges = array();
        for($i = 0; $i < count($array); $i++){
            $rstart = $array[$i];
            $rend = $rstart;
            while($array[$i + 1] - $array[$i] == 1){
                $rend = $array[$i + 1]; //incremenent the index if sequential
                $i++;
            }
            $ranges[] = ($rstart == $rend) ? $rstart.'' : $rstart . '-' . $rend;
        }
        return $ranges;
    }
    
    var_dump(getRanges(array(2,3,4,5,10,18,19,20)));
    /*
    array(3) {
      [0]=>
      string(3) "2-5"
      [1]=>
      string(2) "10"
      [2]=>
      string(5) "18-20"
    }
    */
    
    ?>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

JavaScript/JQuery var arr=[]; $('.element').each(function(i) { arr.push({id:i,value:$(this).attr('data-value')}); }); $.get('/json.php?arr='+arr,function(result) { alert(result); }); PHP <?php $j
This isn't a javascript question but I've created a fiddle so I can demonstrate
Sorry if the title isn't clear enough, but in Javascript you can do this
javascript code: function getCheckbox(name,id){ var check = 0; var deger = option-checkbox-+id; var dom
JavaScript: $('.addTable').live('click', function () { var appendTxt = <table id='tab1'> <tr><td><input type='text' name='input1' /></td><td><input
First, I'm not a javascript developer so I don't have a great deal of
Not even really sure what kind of title this question needs. Basically I have
I have some client-side JavaScript validation on a form. It works great. But I
I normally set up my javascript code to have a function. But due to
I've already found this question on SO (which is exactly the same problem I'm

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.