
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Convert to Hex and Sum Numeral Part in JavaScript
Problem
We are required to write a JavaScript function that takes in a string. Our function should convert every character of the string to the hex value of its ascii code, then the result should be the sum of the numbers in the hex strings ignoring the letters present in hex.
Example
Following is the code −
const str = "Hello, World!"; const toHexAndSum = (str = '') => { return str .split('') .map(c=>c.charCodeAt()) .map(n=>n.toString(16)) .join('') .split('') .filter(c=>'123456789'.includes(c)) .map(d=>parseInt(d)) .reduce((a, b)=>a+b, 0) }; console.log(toHexAndSum(str));
Output
Following is the console output −
91
Advertisements