Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Auto Save

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <b>Output:</b><br>
<div id="output"></div>

<div class="explain">

  Overall, this code follows the Open/Closed Principle by encapsulating the merging logic in a separate class (IntervalMerger) and method (merge) while allowing for easy extension and modification of the merging behavior in the future without modifying the existing code.<br><br>

  <b>IntervalMerger Class:</b><br>
  A class called IntervalMerger that will encapsulate the logic for merging intervals.<br><br>

  <b>merge() Method:</b><br>
  Within the IntervalMerger class, we have a merge method that takes an array of intervals as input and merges them according to the merging logic.<br><br>

  <b>Merging Logic:</b><br>
  The merging logic sorts the intervals based on their Array element position and iterates through them, merging overlapping or adjacent intervals.<br><br>

  <b>Merging Loop:</b><br>
  We iterate over the sorted intervals, comparing each interval with the previous one to determine if they can be merged or not.<br><br>

  <b>Merging Intervals:</b><br>
  If the current interval can be merged with the previous one (i.e., its start Array element position is less than or equal to the end element position of the previous interval), we update the end time of the previous interval to be the maximum of the two end times. This effectively merges the intervals.
  If the intervals cannot be merged, we add the current interval to the list of merged intervals.<br><br>

  <b>Returning Merged Intervals:</b><br>
  The return function of merged intervals.<br><br>

  <b>Usage:</b><br>
  Create an instance of IntervalMerger and call the merge method with the input array of intervals to obtain the merged intervals.<br><br>

  <b>Output:</b><br>
  The merged intervals are logged to the console and HTML.

</div>
              
            
!

CSS

              
                body {
  font-family: arial;
}
#output {
  background-color: black;
  color: white;
  padding: 0.5em;
  border-radius: 0.5em;
}
.explain {
  margin-top: 1em;
}

              
            
!

JS

              
                // In the context of merging the intervals Array, using the SOLID (OCP) principles by creating an IntervalMerger class that follows the (OCP) rules that suggests that software entities (classes, modules, functions, etc.) should be open for extension but closed for modification.

class IntervalMerger {
  merge(intervals) {
    if (intervals.length <= 1) return intervals;
    
    const mergedIntervals = [];
    intervals.sort((a, b) => a[0] - b[0]);
    
    intervals.forEach(function (currentInterval, i) {
      
      const previousInterval = //Conditional (ternary) operator
        i > 0 ? mergedIntervals[mergedIntervals.length - 1] : 0;
      
      if (currentInterval[0] <= previousInterval[1]) {
        previousInterval[1] = Math.max(previousInterval[1], currentInterval[1]);
      } else {
        mergedIntervals.push(currentInterval);
      }
      
    });
    
    return mergedIntervals;
  }
}

const intervals = [[1, 3],[2, 6],[8, 10],[15, 18]];
const intervalMerger = new IntervalMerger();
const updatedIntervals = intervalMerger.merge(intervals);

// output
console.log(updatedIntervals);
console.log(`Updated Intervals: ${JSON.stringify(updatedIntervals)}`);
document.getElementById("output").innerHTML = `Updated Intervals: ${JSON.stringify(updatedIntervals)}`;


// old version 

/*
class IntervalMerger {
    merge(intervals) {
        if (intervals.length <= 1) return intervals;

        intervals.sort((a, b) => a[0] - b[0]);

        const mergedIntervals = [intervals[0]];

        for (let i = 1; i < intervals.length; i++) {
            const currentInterval = intervals[i];
            const previousInterval = mergedIntervals[mergedIntervals.length - 1];

            if (currentInterval[0] <= previousInterval[1]) {
                previousInterval[1] = Math.max(previousInterval[1], currentInterval[1]);
            } else {
                mergedIntervals.push(currentInterval);
            }
        }

        return mergedIntervals;
    }
}
*/
              
            
!
999px

Console