

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
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
Converting to hex and summing the 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
- Related Questions & Answers
- Converting decimal to binary or hex based on a condition in JavaScript
- ASCII to hex and hex to ASCII converter class in JavaScript
- Summing up digits and finding nearest prime in JavaScript
- Constructing an object from repetitive numeral string in JavaScript
- Summing numbers from a string - JavaScript
- Summing up unique array values in JavaScript
- Converting array to set in JavaScript
- Converting degree to radian in JavaScript
- Converting ASCII to hexadecimal in JavaScript
- Summing up to amount with fewest coins in JavaScript
- Converting humanYears into catYears and dogYears in JavaScript
- CSS speak-numeral property
- Summing all the unique values of an array - JavaScript
- Converting strings to uppercase and lowercase with vanilla JavaScript
- Generating random hex color in JavaScript
Advertisements