v1
9/3/2020 by nikkypizza -00
Setup HTML - click to add setup HTML
disable setup JavaScript
Setup JavaScript
const testCases = [
  {test:[1,3,2,14,6,11,5,13,12], result: '1-3, 5, 6, 11-14'},
  {test:[7,8,9,1,4,0,-1,5], result: '-1-1, 4, 5, 7-9'},
  {test:[1,3,2,14,6,11,5,13,12,0,15,17], result: '0-3, 5, 6, 11-15, 17'},
  {test:[7,8,9,1,4,0,-1,5], result: '-1-1, 4, 5, 7-9'},
  {test:[-77,-78,-79,-10,-40,0,-1,-2], result: '-79--77, -40, -10, -2-0'},
  {test:[-77,-78,-79,-10,-40,0,-3,-2,-4], result: '-79--77, -40, -10, -4--2, 0'},
  {test:[17, 16, 14, 13, 11, 10, 9, 0, 1, 3, 7, 8], result:'0, 1, 3, 7-11, 13, 14, 16, 17'},
  {test:[1], result: '1'},
  {test:[], result: ''},
  {test:undefined, result: ''}
];

runTests = (cb) => {
  testCases.forEach((item, index) => {
  	Object.freeze(item);
    const result = cb(item.test);
    console.assert(result===item.result, {result, errorMsg:`Test${index +1}: expected ${item.result}`});
  });
}
delete caserun single casemove downdrag and drop case


ready



function getIntervals(arr) {
  if (!arr || !arr.length) return '';
  if (arr.length === 1) return `${arr[0]}`;

  const sortedNumbers = [...arr].sort((a,b) => a - b);
  let result = `${sortedNumbers[0]}`;
  let sequence = 0;
  const getDelimiter = (seq) => (seq === 1)? ', ': '-';

  for (let i = 1; i < sortedNumbers.length; i++) {
    const [prevoius,current,next] = [sortedNumbers[i-1], sortedNumbers[i], sortedNumbers[i+1]];
    if ((current - prevoius === 1)) {
      ++sequence;
      if (next === void 0) return `${result}${getDelimiter(sequence)}${current}`
      continue;
    } 
    result += `${sequence ? `${getDelimiter(sequence)}${prevoius}`: ``}, ${current}`;
    sequence = 0;  
  }

  return result;
}

runTests(getIntervals);
delete caserun single casemove upmove downdrag and drop case


ready



const getIntervals = (arr) => {
  if(!Array.isArray(arr)) return '';

  const result = [];
  let temp = [];

  arr.sort((a, b) => a - b);

  for (let i = 0; i < arr.length; i++) {
    temp.push(arr[i]);

    if (arr[i] + 1 !== arr[i + 1]) {
      temp.length > 2 ?
        result.push(`${temp[0]}-${temp[temp.length - 1]}`) :
        result.push(...temp);

      temp = [];
    }
  }
  return result.join(', ');
}

runTests(getIntervals);
delete caserun single casemove updrag and drop case


ready



const getIntervals = (list) => {
  if (!list || !list.length ) {
    return '';
  }

  let count = 0;
  const sortedList = list.sort((a, b) => a - b);
  const template = sortedList.map((item, index) => {
    if (sortedList[index + 1] === item + 1) {
      count++;
    } else {
      count = 0
    }

    return {value: item, count};
  });

  return template.reduce((acc, item, index) => {
    const stringEnd = index === template.length - 1 ? '' : ', ';

    if (item.count === 0) {
      if (!template[index - 1] || template[index - 1].count === 0) {
        acc +=  `${item.value}${stringEnd}`;
      } else if (template[index - 1].count === 1) {
        acc += `${template[index - 1].value}, ${item.value}${stringEnd}`;
      } else {
        acc += `${item.value - template[index - 1].count}-${item.value}${stringEnd}`;
      }
    }

    return acc;
  }, ``);
}

runTests(getIntervals);
Test Case - click to add another test case
Teardown JS - click to add teardown JavaScript
Output (DOM) - click to monitor output (DOM) while test is running
RUN