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"]
6
  • 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 +. Commented Apr 19, 2010 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. Commented Apr 19, 2010 at 15:38
  • i was confused by this bit, particularly: var ranges = [], rstart, rend; Commented Apr 19, 2010 at 15:38
  • 1
    $ranges=array(); - you don't have to define beginning and end of an array in php. Commented Apr 19, 2010 at 15:39
  • 1
    "var ranges = [], rstart, rend;" is just saying "make three variables, one called ranges that is an array and two called "rstart" and "rend". Commented Apr 19, 2010 at 15:44

2 Answers 2

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

?>
Sign up to request clarification or add additional context in comments.

4 Comments

... or maybe it is a place for that.
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.
Thanks Austin, very kind of you to take the time I'll be sure to be less lazy with questions in future! thanks again
Yeah, the code is so analogous it probably only took a minute.
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.