Turn each character into its ASCII character code and join them together to create a number in JavaScript

Problem

We need to write a JavaScript function that takes a string and converts each character to its ASCII code, joins them to create a number, replaces all instances of 7 with 1, and returns the difference between the original and modified numbers.

Understanding the Process

The algorithm involves several steps:

  1. Convert each character to its ASCII code using charCodeAt()
  2. Join all ASCII codes to form one large number
  3. Replace all occurrences of digit 7 with 1
  4. Calculate the difference between original and modified numbers

Complete Implementation

const str = 'AVEHDKDDS';

const ASCIIDifference = (str = '') => {
    // Step 1: Convert each character to ASCII and join
    const asciiString = str
        .split('')
        .map(c => c.charCodeAt(0))
        .join('');
    
    console.log("ASCII string:", asciiString);
    
    // Step 2: Replace all 7s with 1s
    const modifiedString = asciiString.replace(/7/g, '1');
    console.log("Modified string:", modifiedString);
    
    // Step 3: Calculate difference
    const originalNumber = parseInt(asciiString);
    const modifiedNumber = parseInt(modifiedString);
    const difference = originalNumber - modifiedNumber;
    
    console.log("Original number:", originalNumber);
    console.log("Modified number:", modifiedNumber);
    console.log("Difference:", difference);
    
    return difference;
};

console.log("Final result:", ASCIIDifference(str));
ASCII string: 65866972686875688368
Modified string: 65866912686815688368
Original number: 6.586697268687569e+19
Modified number: 6.586691268681569e+19
Difference: 600006000000
Final result: 600006000000

Step-by-Step Breakdown

// Let's trace through with a simpler example
const simpleStr = 'ABC';

console.log("Original string:", simpleStr);

// Step 1: Get ASCII codes
const asciiCodes = simpleStr.split('').map(c => c.charCodeAt(0));
console.log("ASCII codes:", asciiCodes);

// Step 2: Join them
const joinedAscii = asciiCodes.join('');
console.log("Joined ASCII:", joinedAscii);

// Step 3: Replace 7s with 1s (if any)
const modified = joinedAscii.replace(/7/g, '1');
console.log("After replacing 7s with 1s:", modified);

// Step 4: Calculate difference
const difference = parseInt(joinedAscii) - parseInt(modified);
console.log("Difference:", difference);
Original string: ABC
ASCII codes: [65, 66, 67]
Joined ASCII: 656667
After replacing 7s with 1s: 656661
Difference: 6

Optimized Version

const ASCIIDifferenceOptimized = (str = '') => {
    const asciiString = str
        .split('')
        .map(c => c.charCodeAt(0))
        .join('');
    
    const modifiedString = asciiString.replace(/7/g, '1');
    
    return parseInt(asciiString) - parseInt(modifiedString);
};

// Test with different strings
console.log(ASCIIDifferenceOptimized('A'));    // ASCII: 65
console.log(ASCIIDifferenceOptimized('G'));    // ASCII: 71, contains 7
console.log(ASCIIDifferenceOptimized('AB'));   // ASCII: 6566
0
60
0

Key Points

  • charCodeAt(0) returns the ASCII value of the first (and only) character
  • join('') concatenates array elements into a single string
  • replace(/7/g, '1') replaces all occurrences of '7' with '1'
  • Large numbers may lose precision when converted to integers

Conclusion

This function demonstrates string manipulation, ASCII conversion, and mathematical operations in JavaScript. The key insight is converting characters to ASCII codes, manipulating the resulting number string, and calculating the difference between original and modified values.

Updated on: 2026-03-15T23:19:00+05:30

443 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements