Convert Number to Corresponding String in JavaScript



Problem

We are required to write a JavaScript function that takes in a number n and converts it to the corresponding string without using the inbuilt functions String() or toString() or using string concatenation.

Example

Following is the code −

 Live Demo

const num = 235456;
const convertToString = (num) => {
   let res = '';
   while(num){
      res = (num % 10) + res;
      num = Math.floor(num / 10);
   };
   return res;
};
console.log(convertToString(num));

Output

Following is the console output −

235456
Updated on: 2021-04-17T10:53:56+05:30

326 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements