- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Reversing negative and positive numbers in JavaScript
Problem
We are required to write a JavaScript function that takes in a number and returns its reversed number.
One thing that we should keep in mind is that numbers should preserve their sign; i.e., a negative number should still be negative when reversed.
Example
Following is the code −
const num = -224; function reverseNumber(n) { let x = Math.abs(n) let y = 0 while (x > 0) { y = y * 10 + (x % 10) x = Math.floor(x / 10) }; return Math.sign(n) * y }; console.log(reverseNumber(num));
Output
-422
- Related Articles
- Implement Bubble sort with negative and positive numbers – JavaScript?
- Distinguish positive and negative numbers.
- Sum a negative number (negative and positive digits) - JavaScript
- Split an array of numbers and push positive numbers to JavaScript array and negative numbers to another?
- Explain the difference between positive and negative numbers.
- Lambda expression in Python to rearrange positive and negative numbers
- Count positive and negative numbers in a list in Python program
- Python program to count positive and negative numbers in a list
- Lambda expression in Python Program to rearrange positive and negative numbers
- Rearrange positive and negative numbers using inbuilt sort function in C++
- Rearrange positive and negative numbers with constant extra space in C++
- How to print positive and negative infinity values in JavaScript?
- Positive, negative and zeroes contribution of an array in JavaScript
- Java Program to convert positive int to negative and negative to positive
- Schizophrenia Symptoms: Positive and Negative?

Advertisements