Numbers to chars in JavaScript and vice versa

Photo by Tim Evans on Unsplash

Recently, I have to solve code problems while searching for job opportunities.

It gave me an array of numbers such as

[104,116,116,112,115,58,47,47,109,101,100,105,117,109,46,99,111,109]

and told me to find what to do.

It was not so difficult after all. But I thought it would be great to learn language concepts.

So I decided to write short posts in JavaScript, Python and Rust to compare .You may bring reusable codes at the end of the post.

[Table of Contents]

  1. What to do?
  2. Turn it into JavaScript Code with FP
  3. Vice Versa(char to numbers)
  4. Don’t repeat yourself with functions
  5. Conclusion

1. What to do?

I thought that I should get some url to reach the next proccess. So I assumed that I have to turn those numbers in characters.

First, I remembered that each numbers and characters have its own counterparts. So I need to tweak every numbers to corresponding characters.

Then, I have to join them into entire url not to type them manually.

Not so difficult when you already knew how to use JavaScript methods for array and string.

So let’s wrtie some code for that.

2. Turn it into JavaScript Code with FP

I thought that JavaScript FP methods would be greate candidate for this case. So I will use them for this example.

You may code with your browser console by ctrl + shift + i or your code editor such as Visual Studio Code etc.

Numbers to Char Example by steadylearner

The above code snippet shows that those numbers were actually a link to some url.

We just made computer to tweak each number in array with map keyword and String.fromCharCode(x) after fat arrow to turn each number to a corresponding character.

Now we figured out where to go. It was https://www.medium.com.

But we wouldn’t want to type each character to do so.

We have to make them into string to easily copy and paste.

So the next phase is to join the array of each chracters to string.

Join array of characters example by steadylearner

We used join JavaScript array method here to solve this.

and now we got url “https://medium.com” and easily go to the next phase.

3. Vice Versa(Characters to numbers)

Photo by Mathieu Stern on Unsplash

Now You got what you wanted, but It is good to know how to do things oppositely at the same time.

Who knows that you would be a recruiter who needs to hire some developers?

Because you already know what matters here, I will include entire code snippets below.

You just need to know that we need codes what do exactly opposite things from the previous example.

They are split and charCodeAt .

(You may search for yourself about split and charCodeAt.)

You may write code snippet like the example below.

String to array of numbes example by steadylearner

4. Don’t repeat yourself with functions

Now You know everything I wanted to write for this article.

But It would be unefficient if you have to type those long words each time.

So you may save those process using fucntions.

encodeSecretChracters example by steadylearner

When you make functiosn to do, You don’t have to repeat what you already know. You just pass the string “https://medium.com” as arguments and that is all.

I want to point out that it is alway better to simple type annotation as I did with (str = "") to help others and computer to understand what happens.

You may make function for decode yourself.

It would be helpful for you to read articles for functions and arrow functions in JavaScript.

5. Conclusion

Photo by Matt Botsford on Unsplash

You did well. I hope this article helped you to learn how to turn numbers into characters and vice versa. You may let comments if there are parts not explained well or not correct.

Thanks for reading and please share this post.

p.s

  1. Did you like my coding style? Work with me. Please, send messages via LinkedIn.
  2. I am also developing React FrontEnd and Rust BackEnd Website www.steadylearner.com It lacks content but I would write it again together with real posts written here.

Further Reading

  1. Numbers to chars in Python and vice versa
  2. Numbers to chars in Rust and vice versa

I am not a native English speaker. When some words don’t make sense, grammar problems etc. Please leave a comment below. I will read this post again and edit them right away.

--

--

--

Visit www.steadylearner.com/blog for more contents. Contact me with https://www.linkedin.com/in/steady-learner-3151b7164 for job offers.

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

6 Common Maintenance Challenges that CMMS Software Helps Resolve

Google foobar as a non-developer

Cucumber BDD testing using TestNG framework with Selenium webdriver and Cucumber reports

How timely migration can save your money?

LeetCode 3. Longest Substring Without Repeating Characters

Lodestar Update: November 2020

Reflections on SCM

Daily blog of the first-time founder #52

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Steadylearner

Steadylearner

Visit www.steadylearner.com/blog for more contents. Contact me with https://www.linkedin.com/in/steady-learner-3151b7164 for job offers.

More from Medium

Dealing With Date Serialization And Deserialization In JavaScript

JavaScript Overview — Functional Programming (Immutability, Side Effects, Pure Functions, HOFs)

Binary Search Tree in JavaScript - (Implementation, Traversing, Formulas)

Using async/await in a forEach loop (you can’t)