
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
Comparing ascii scores of strings - 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.
Example
Let's write the code for this −
const str1 = 'This is the first string.'; const str2 = 'This here is the second string.'; const calculateScore = (str = '') => { return str.split("").reduce((acc, val) => { return acc + val.charCodeAt(0); }, 0); }; const compareASCII = (str1, str2) => { const firstScore = calculateScore(str1); const secondScore = calculateScore(str2); return Math.abs(firstScore - secondScore); }; console.log(compareASCII(str1, str2));
Output
Following is the output in the console −
536
- Related Articles
- ASCII sum difference of strings in JavaScript
- Comparing Strings and Portions of Strings in Java
- Comparing two strings in MySQL?
- Comparing two strings in C++
- Python – Filter Strings within ASCII range
- Comparing two Strings lexicographically in Java \n
- Comparing Strings with (possible) null values in java?
- How to remove non-ASCII characters from strings
- Minimum ASCII Delete Sum for Two Strings in C++
- Comparing corresponding values of two arrays in JavaScript
- Comparing adjacent element and swap - JavaScript?
- Comparing and filling arrays in JavaScript
- ASCII to hex and hex to ASCII converter class in JavaScript
- Finding the ASCII score of a string - JavaScript
- Interpretation of Test Scores

Advertisements