Corner digit number difference - JavaScript

JavascriptWeb DevelopmentFront End TechnologyObject Oriented Programming

We are required to write a JavaScript function that takes in a number, constructs a new number from the first and last digit of that number and returns the difference between the original number and the number thus formed.

For example: If the input is 34567

Then the corner digits number will be −

37

And the output will be −

34530

Example

Following is the code −

const num = 34567;
const cornerDifference = num => {
   let temp = Math.abs(num);
   let corner = temp % 10;
   if(temp < 100){
      corner = temp;
   }else{
      while(temp >= 10){
         temp = Math.floor(temp / 10);
      };
      corner = (temp*10) + corner;
   };
   return num - corner;
};
console.log(cornerDifference(num));

Output

Following is the output in the console −

34530
raja
Published on 15-Sep-2020 11:56:41
Advertisements