Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
ASCII sum difference of strings in JavaScript
ASCII (American Standard Code for Information Interchange) is a 7-bit character encoding standard where every character has a unique decimal code. We can calculate the ASCII sum of strings and find their difference in JavaScript.
Understanding ASCII Codes
Each character has a specific ASCII value. For example, 'A' = 65, 'a' = 97, '0' = 48, and space = 32. We can get these values using JavaScript's charCodeAt() method.
Getting ASCII Values
console.log('A'.charCodeAt(0)); // 65
console.log('a'.charCodeAt(0)); // 97
console.log('0'.charCodeAt(0)); // 48
console.log(' '.charCodeAt(0)); // 32 (space)
65 97 48 32
Calculating ASCII Sum of a String
To find the ASCII sum, we split the string into characters and add up their ASCII values:
const calculateASCIISum = (str = '') => {
return str.split('').reduce((acc, char) => {
return acc + char.charCodeAt(0);
}, 0);
};
// Test with simple strings
console.log(calculateASCIISum('ABC')); // 65 + 66 + 67 = 198
console.log(calculateASCIISum('Hello')); // Sum of H+e+l+l+o ASCII values
198 500
Finding ASCII Sum Difference
Now we can create a function that calculates the absolute difference between two strings' ASCII sums:
const calculateASCIISum = (str = '') => {
return str.split('').reduce((acc, char) => {
return acc + char.charCodeAt(0);
}, 0);
};
const getASCIIDifference = (str1, str2) => {
const firstSum = calculateASCIISum(str1);
const secondSum = calculateASCIISum(str2);
console.log(`"${str1}" ASCII sum: ${firstSum}`);
console.log(`"${str2}" ASCII sum: ${secondSum}`);
return Math.abs(firstSum - secondSum);
};
// Example with different strings
const string1 = 'Hello';
const string2 = 'World';
console.log(`ASCII difference: ${getASCIIDifference(string1, string2)}`);
"Hello" ASCII sum: 500 "World" ASCII sum: 552 ASCII difference: 52
Complete Example
Here's a practical example with longer strings:
const str1 = 'This is an example string';
const str2 = 'This is the second string';
const calculateASCIISum = (str = '') => {
return str.split('').reduce((acc, char) => {
return acc + char.charCodeAt(0);
}, 0);
};
const getASCIIDifference = (str1, str2) => {
const firstSum = calculateASCIISum(str1);
const secondSum = calculateASCIISum(str2);
return Math.abs(firstSum - secondSum);
};
console.log(`String 1: "${str1}"`);
console.log(`String 2: "${str2}"`);
console.log(`ASCII sum difference: ${getASCIIDifference(str1, str2)}`);
String 1: "This is an example string" String 2: "This is the second string" ASCII sum difference: 116
Alternative Using for...of Loop
You can also calculate ASCII sum using a for...of loop instead of reduce:
const calculateASCIISumLoop = (str) => {
let sum = 0;
for (const char of str) {
sum += char.charCodeAt(0);
}
return sum;
};
console.log(calculateASCIISumLoop('Test')); // ASCII sum using loop
416
Conclusion
Use charCodeAt(0) to get ASCII values and reduce() to sum them efficiently. The Math.abs() function ensures you get a positive difference between string ASCII sums.
