Task to be performed in JavaScript

Generally I will be given a value separated by commas

Example: 1,2,3,4,5
Output: 1…5

Example2: 1,2,3,5,6,7,9
Output: 1…3,5…7,9

Starting and ending number of consecutive numbers shld be separated by comma… Do u guys have any idea of how to do this??

I solved it… Thanks for ur idea @kylec

<script>
        let value = "21,22,23,24,25,29,28,31,35,37,38,39", result = "", similar_values = [];
        let array_value = [];
        (function printvalue() {
            console.log(value);
            array_value = value.split(',').map(Number).sort();
            for(let i=0;i<array_value.length;i++) {
                if(array_value[i]+1 == array_value[i+1]) {
                    similar_values.push(array_value[i]);
                }
                else {
                    if(similar_values.length>0) {
                        similar_values.push(array_value[i]);
                        let start = '', end='';
                        [start, end] = [similar_values[0], similar_values[similar_values.length -1]];
                        similar_values = [];
                        result += start+'...'+end+',';
                    }
                    else {
                        result += array_value[i] + ',';
                    }
                }
            }
            result= result.replace(/^,|,$/g,'');
            console.log(result);
        })();
    </script>

What’s the logic behind replacing numbers with ellipses?
How many numbers should be removed?

Are the inputs/output both strings?

What have you tried so far?

Your questions lack a lot of missing pieces to be given an answer :slight_smile:

I would start by changing your list of values to an Array, (we’ll call it list) if it isn’t already. This might mean splitting, String.split(','), your list if it’s a string and potentially changing each string value into a Number type. If the list of values isn’t in numerical order to start with, it may need to be sorted, Array.sort().
Next I would create two empty arrays, result & currentSet.
Using a for loop, iterate through list:

  1. push the current list value to currentSet
  2. Ask:
  • If this happens to be the last value in list, push currentSet to result
  • else if the current list value plus one does not equal the next list value: push currentSet to result then rest currentSet to an empty state.

Finally. Join, Array.join(' '), each item in each consecutive set of result with whatever separator you’d like as a string, and join each consecutive set together with a comma.

1 Like

I will update my question @Marmiz

I am trying it and will let u know if I got any problem :slight_smile:

Final Solution

<script>
        let value = "21,22,23,24,25,29,28,31,35,37,38,39", result = "", similar_values = [];
        let array_value = [];
        (function printvalue() {
            console.log(value);
            array_value = value.split(',').map(Number).sort();
            for(let i=0;i<array_value.length;i++) {
                if(array_value[i]+1 == array_value[i+1]) {
                    similar_values.push(array_value[i]);
                }
                else {
                    if(similar_values.length>0) {
                        similar_values.push(array_value[i]);
                        let start = '', end='';
                        [start, end] = [similar_values[0], similar_values[similar_values.length -1]];
                        similar_values = [];
                        result += start+'...'+end+',';
                    }
                    else {
                        result += array_value[i] + ',';
                    }
                }
            }
            result= result.replace(/^,|,$/g,'');
            console.log(result);
        })();
    </script>