Convert number to characters in JavaScript


In the given problem statement we are asked to convert numbers to characters with the help of javascript functionalities. In Javascript we have some built−in functions to convert a number to its corresponding characters and also with user defined functions we can convert it.

Logic for The Above Problem

As we know that every programming language defines its own function to perform certain operations. So Javascript has a built−in method to convert a number into its corresponding character. These function names are 'fromCharCode' and 'charCodeAt'. With these functions we need to pass the input number and it will convert and give us the required output in character form.

For example

const n = 65;
const c = String.fromCharCode(n);
console.log(c); 
// Output: "A"

Let's understand the above example in detail. In the code we have defined a number which is storing 65 in it and then we have used fromCharCode() to convert this number into character. After conversion from this function the output will be 'A'. Because 65 represents A in character form.

Algorithm

Step 1 − For solving this problem statement we are using an integer array. This array will be passed to the function for conversion into characters.

Step 2 − After completing the first step lets define another variable called char to store the resultant characters in it.

Step 3 − So for converting each number in character we are using a for loop in which will run until the length of the array.

Step 4 − For conversion of array numbers we are using String.formCharCode method of Javascript in this step and pass every element in it.

Step 5 − Now conversion has done and put converted characters in char variable which has been defined in step 2.

Step 6 − At the end display all the characters in the console.

Example

//define the numbers in array form
const numArray = [68, 69, 70];
let chars = "";
//use for loop to convert each number into character
for (let i = 0; i < numArray.length; i++) {
  chars += String.fromCharCode(numArray[i]);
}
console.log(chars);

Output

DEF

In the above code, the array of numbers [68, 69, 70] is converted to its corresponding characters "DEF" using a for loop and the String.fromCharCode() method. We can see here we are passing the consecutive numbers in the function and after conversion of each number 68 represents D, 69 represents E and 70 represents F.

Complexity

The time complexity for converting a single number to its equivalent character is constant time of O(1), because it only calls the built−in function called String.fromCharCode().

The complexity for the code to convert numbers of array to their corresponding characters is linear time which is O(n) in this n is the number of items in an array. This code also uses a for loop to iterate all the elements of the array and to convert it in characters each time fromCharCode method is called

Conclusion

We have used a predefined method fromCharCode() of Javascript. And we have also seen the time complexity for above codes as the first example is only taking constant time and another method is using O(n) time because of the for loop.

Updated on: 22-Aug-2023

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements