CrackFrontendCF
Resources
Practice
CrackFrontend

Your guide to mastering frontend interviews

Resources

LibraryPracticeDonate

Company

About UsContact

Legal

Privacy PolicyTerms of Service

Built with ❀️ by Tushar Khanna

CrackFrontendCF
Resources
Practice

πŸ“„ Document Comparison (Diff)

Line-by-line and word-by-word document comparison utility. Essential for version control, collaborative editing, and content management systems.

Document comparison (diff) is essential for version control, collaborative editing, and content management systems. This implementation compares two text documents and returns structured line-by-line and word-by-word differences. -- βœ… Implementation -- πŸ” Example Usage -- 🧾 Output (Structured) -- πŸ’‘ Notes No external dependencies Simple granularity: line word Expandable for char-level or structural diffing (e.g. JSON, HTML trees) -- <!-quiz-start --Q1: What does the function return when two lines are identical? [ ] [x] [ ] [ ] An empty object Q2: How are words compared within a differing line? [ ] Using character-by-character comparison [x] By splitting on whitespace and comparing each word by index [ ] Using regular expressions [ ] By calculating Levenshtein distance Q3: What happens when one document has more lines than the other? [ ] The function throws an error [ ] Extra lines are ignored [x] Missing lines are treated as empty strings in the comparison [ ] The function returns null <!-quiz-end --
JavaScriptUtilities
βž• Chained Sum (Curried Function)
medium
⏱️ Debounce Function in JavaScript
medium
πŸ“‹ Deep Clone Implementation
easy
πŸ”„ distinctUntilChanged() Polyfill
easy
πŸ“„ Document Comparison (Diff)
easy
πŸ“’ Custom EventEmitter Implementation
hard
πŸ“¦ Flatten Object Implementation
medium
🐫➑️🐍 Converting camelCase to snake_case in JavaScript (Without Regex)
easy
πŸ”„ mapLimit: Controlled Concurrency in JavaScript
medium
⚑️ Fire on Push: Dispatching Custom Events When an Array Changes in JavaScript
medium
πŸ”„ Removing Circular References from Objects
hard
πŸ“Š Sampling Function: Execute Once Every N Calls
medium
⏱️ Throttle Function in JavaScript
medium
πŸ”„ undefinedToNull Utility
medium
5 of 14
LibraryJavaScriptUtilities52 of 61

πŸ“„ Document Comparison (Diff)

jsutilseasy

Document comparison (diff) is essential for version control, collaborative editing, and content management systems. This implementation compares two text documents and returns structured line-by-line and word-by-word differences.


βœ… Implementation

function compareDocuments(doc1, doc2) {
  const lines1 = doc1.split('\n');
  const lines2 = doc2.split('\n');

  const maxLines = Math.max(lines1.length, lines2.length);
  const diffs = [];

  for (let i = 0; i < maxLines; i++) {
    const line1 = lines1[i] || '';
    const line2 = lines2[i] || '';

    if (line1 === line2) {
      diffs.push({ line: i + 1, status: 'equal', content: line1 });
    } else {
      diffs.push({
        line: i + 1,
        status: 'different',
        content1: line1,
        content2: line2,
        wordDiffs: compareWords(line1, line2)
      });
    }
  }

  return diffs;
}

function compareWords(str1, str2) {
  const words1 = str1.split(/\s+/);
  const words2 = str2.split(/\s+/);
  const maxWords = Math.max(words1.length, words2.length);
  const wordDiffs = [];

  for (let i = 0; i < maxWords; i++) {
    const w1 = words1[i] || '';
    const w2 = words2[i] || '';
    wordDiffs.push({
      index: i,
      word1: w1,
      word2: w2,
      status: w1 === w2 ? 'equal' : 'different'
    });
  }

  return wordDiffs;
}

πŸ” Example Usage

const doc1 = `The quick brown fox
jumps over the lazy dog.`;

const doc2 = `The quick brown fox
leaps over the sleepy dog.`;

console.log(compareDocuments(doc1, doc2));

🧾 Output (Structured)

[
  {
    "line": 1,
    "status": "equal",
    "content": "The quick brown fox"
  },
  {
    "line": 2,
    "status": "different",
    "content1": "jumps over the lazy dog.",
    "content2": "leaps over the sleepy dog.",
    "wordDiffs": [
      { "index": 0, "word1": "jumps", "word2": "leaps", "status": "different" },
      { "index": 1, "word1": "over", "word2": "over", "status": "equal" },
      { "index": 2, "word1": "the", "word2": "the", "status": "equal" },
      { "index": 3, "word1": "lazy", "word2": "sleepy", "status": "different" },
      { "index": 4, "word1": "dog.", "word2": "dog.", "status": "equal" }
    ]
  }
]

πŸ’‘ Notes

  • No external dependencies
  • Simple diff granularity: line + word
  • Expandable for char-level or structural diffing (e.g. JSON, HTML trees)

Quick Quiz

Test your understanding with 3 quick questions

Q1What does the `compareDocuments` function return when two lines are identical?
Q2How are words compared within a differing line?
Q3What happens when one document has more lines than the other?

Continue Reading

CrackFrontend

Your guide to mastering frontend interviews

Resources

LibraryPracticeDonate

Company

About UsContact

Legal

Privacy PolicyTerms of Service

Built with ❀️ by Tushar Khanna