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


Problem

We are required to write a JavaScript function that takes in a string. Our function should turn each character into its ASCII character code and join them together to create a number. Then we should replace all instances of 7 from this number to 1 to construct another number. Finally, we should return the difference of both these numbers

Example

Following is the code −

 Live Demo

const str = 'AVEHDKDDS';
const ASCIIDifference = (str = '') => {
   return str
   .split('')
   .map(c => c.charCodeAt(0))
   .join('')
   .split('')
   .map(Number)
   .filter(str => str === 7)
   .length * 6;
};
console.log(ASCIIDifference(str));

Output

12

Updated on: 19-Apr-2021

166 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements