Finding the ASCII score of a string - 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 JavaScript function that takes in a string and counts the sum of all the ascii codes of the string characters

Example

Following is the code −

const str = 'This string will be used for calculating ascii score';
const calculateASCII = str => {
   let res = 0;
   for(let i = 0; i < str.length; i++){
      const num = str[i].charCodeAt(0);
      res += num;
   };
   return res;
};
console.log(calculateASCII(str));

Output

Following is the output in the console −

4946

Updated on: 18-Sep-2020

248 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements