-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathrange_extraction.js
More file actions
42 lines (40 loc) · 2.38 KB
/
range_extraction.js
File metadata and controls
42 lines (40 loc) · 2.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
/******************************************************************************************
* CODEWARS RANGE EXTRACTION CHALLENGE *
* *
* Problem Statement *
* A format for expressing an ordered list of integers is to use a comma separated list *
* of either *
* individual integers *
* or a range of integers denoted by the starting integer separated from the end integer *
* in the range by a dash, '-'. The range includes all integers in the interval including *
* both endpoints. It is not considered a range unless it spans at least 3 numbers. *
* For example ("12, 13, 15-17") *
* Complete the solution so that it takes a list of integers in increasing order & returns*
* a correctly formatted string in the range format. *
*****************************************************************************************/
function solution(array){
let sortedArray = array.sort((a, b) => {return a-b});
let rangeExtraction = new String('');
let rangeCount = 0;
for(let i=0; i<sortedArray.length; i++){
rangeCount = 0;
if(sortedArray[i] + 1 !== sortedArray[i + 1]) {
if(i!==sortedArray.length-1) rangeExtraction += sortedArray[i] + String.fromCharCode(44);
else rangeExtraction += sortedArray[i];
}
else if(sortedArray[i] + 1 === sortedArray[i + 1]) {
for(var j=i; j<sortedArray.length; j++){
rangeCount++;
if(sortedArray[j] + 1 === sortedArray[j+1]) continue;
else break;
}
if(rangeCount >= 3){
if(j!==sortedArray.length-1) rangeExtraction += sortedArray[i] + String.fromCharCode(45) + sortedArray[j] + String.fromCharCode(44);
else rangeExtraction += sortedArray[i] + String.fromCharCode(45) + sortedArray[j];
i = j;
}
else rangeExtraction += sortedArray[i] + String.fromCharCode(44);
}
}
return rangeExtraction;
}