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"]
Community
  • 1
  • 1
Haroldo
  • 36,607
  • 46
  • 127
  • 169
  • 4
    There is nothing really JS specific in it, it is nearly the same in PHP. All variables have to start with `$`, array initialization is `array()`, `array.length` translates to `count($array)`, `array.push` is either `$array[]=` or `array_push($array, $value)` and concatenation goes with `.` instead of `+`. – Felix Kling Apr 19 '10 at 15:37
  • 1
    Which parts are you having trouble with? This is a place to get help with your programming. Not to get free labor. – webbiedave Apr 19 '10 at 15:38
  • i was confused by this bit, particularly: var ranges = [], rstart, rend; – Haroldo Apr 19 '10 at 15:38
  • 1
    `$ranges=array();` - you don't have to define beginning and end of an array in php. – dusoft Apr 19 '10 at 15:39
  • var ranges = [], rstart, rend; is just declaring three variables on one line, just like in php. You can chop it up into three separate lines if you wish. – webbiedave Apr 19 '10 at 15:43
  • 1
    "var ranges = [], rstart, rend;" is just saying "make three variables, one called ranges that is an array and two called "rstart" and "rend". – Austin Fitzpatrick Apr 19 '10 at 15:44

2 Answers2

2

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"
}
*/

?>
Austin Fitzpatrick
  • 7,243
  • 3
  • 25
  • 22
  • 2
    ... or maybe it is a place for that. – webbiedave Apr 19 '10 at 15:44
  • 1
    Yeah, I thought about not answering but sometimes it helps to have someone spell it out for you a couple times when you're new. It wasn't much "work" for me, find and replace the variable names with a "$" infront and replace "+" with "." in a few places. – Austin Fitzpatrick Apr 19 '10 at 15:53
  • Thanks Austin, very kind of you to take the time I'll be sure to be less lazy with questions in future! thanks again – Haroldo Apr 19 '10 at 15:55
  • Yeah, the code is so analogous it probably only took a minute. – webbiedave Apr 19 '10 at 15:56
0

Just for your information:

var ranges = [], rstart, rend;

just declares three variables ranges, rstart and rend. ranges is also initialized as an empty array.
It is the same as

var ranges = [];
var rstart;
var rend;

In PHP you don't necessarily have to declare the variables beforehand.

Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143