- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
ASCII sum difference of strings in JavaScript
ASCII Code:
ASCII is a 7-bit character code where every single bit represents a unique character. Every English alphabet has a unique decimal ascii code.
We are required to write a function that takes in two strings and calculates their ascii scores (i.e., the sum of ascii decimal of each character of string) and returns the difference.
Let’s write the code for this function −
Example
The code for this will be −
const str1 = 'This is an example sting'; const str2 = 'This is the second string'; const calculateScore = (str = '') => { return str.split("").reduce((acc, val) => { return acc + val.charCodeAt(0); }, 0); }; const ASCIIDifference = (str1, str2) => { const firstScore = calculateScore(str1); const secondScore = calculateScore(str2); return Math.abs(firstScore - secondScore); }; console.log(ASCIIDifference(str1, str2));
Output
The output in the console −
116
- Related Articles
- Comparing ascii scores of strings - JavaScript
- Minimum ASCII Delete Sum for Two Strings in C++
- Python – Filter Strings within ASCII range
- Difference between two strings JavaScript
- Difference between sum of square and square of sum in JavaScript
- How to remove non-ASCII characters from strings
- ASCII to hex and hex to ASCII converter class in JavaScript
- Difference between ANSI and ASCII
- Product sum difference of digits of a number in JavaScript
- Maximum absolute difference of the length of strings from two arrays in JavaScript
- Converting ASCII to hexadecimal in JavaScript
- Difference between sum and product of an array in JavaScript
- Finding the ASCII score of a string - JavaScript
- Greatest sum and smallest index difference in JavaScript
- Difference between product and sum of digits of a number in JavaScript

Advertisements