Unexpected result when converting decimal to fraction - javascript

function gcd(a, b) {
return (b) ? gcd(b, a % b) : a;
}
var dec2Frac = function (d) {
var top = d.toString().replace(/\d+[.]/, '');
var bot = Math.pow(10, top.length);
if (d > 1) {
top = +top + Math.floor(d) * bot;
}
var x = gcd(top, bot);
var r1 = top / x;
var r2 = bot / x;
var frac = r1 + "/" + r2;
var parts = frac.split('/');
var simpler = parts[0][0]+'/'+parts[1][0];
return simpler;
};
If I input 640x960 = 0.66666666666667
I'm expecting the result to be 2/3 as evident here: http://www.mindspring.com/~alanh/fracs.html
Instead this function returns 6/1. Test here: http://jsbin.com/asoxud/1/

As an addition to MvG's Answer,
I found this quite interesting and wanted to understand how floating points are stored and how to get back an fraction of a float to maybe do calculations with them.
It gave a bit of brainache trying to figure this out on my own, but as it made click, i came up with this Fraction function,
I don't know if this helps you or not, but
now that its written anyway , why not leave it here
function Fraction(n, d) {
if ("number" !== typeof n)
throw new TypeError("Excptected Parameter to be of type number");
var strings = n.toString(2).split("."); //Split the number by its decimal point
if (strings.length > 1 && !d) { //No denominator given and n is a float
var floats = [strings[1].substr(0, 27), strings[1].substr(27, 54)]; //Split into to parts
var int64 = [
parseInt(floats[0], 2) << 1,
parseInt(floats[1], 2) << 1
];
var denominator = Math.pow(2, strings[1].length + 1); //
var numerator = int64[0] * Math.pow(2, floats[1].length);
numerator += int64[1];
numerator += parseInt(strings[0], 2) * denominator;
this.numerator = numerator;
this.denominator = denominator;
this.reduce();
this.approx = approx(n);
} else if (strings.length < 2 && !d) { // If no denominator and n is an int
this.numerator = n;
this.denominator = 1;
} else { //if n and d
this.numerator = n;
this.denominator = d;
}
function approx(f, n) {
n = n || 0;
var fraction = new Fraction(1, 1);
var float = Math.pow(f, -1);
var rec = ~~float;
var decimal = float - rec;
if (float.toPrecision(Fraction.precision) == rec)
return new Fraction(1, rec);
var _fraction = approx(decimal, n + 1);
fraction.denominator = rec * _fraction.denominator + _fraction.numerator;
fraction.numerator = _fraction.denominator;
return fraction;
}
}
//The approx precision
Fraction.precision = 10;
Fraction.prototype.toString = function () {
return this.numerator + "/" + this.denominator;
};
Fraction.prototype.gcd = function () {
return (function gcd(u, v) {
return ((u > 0) ? gcd(v % u, u) : v);
})(this.numerator, this.denominator);
};
Fraction.prototype.reduce = function () {
var _gcd = this.gcd();
this.numerator /= _gcd;
this.denominator /= _gcd;
};
Fraction.prototype.valueOf = function () {
return this.numerator / this.denominator;
};
var f = new Fraction(0.3333);
+ f; //0.3333333333
f.toString(); // 6004799502560181/18014398509481984
+ f.approx //0.33333
+ f.approx.toString() //3333/10000
var g = new Fraction(2 / 3);
+ g; //0.6666666666666666
g.toString(); //6004799503160661/9007199254740992
+ g.approx //0.6666666666666666
+ g.approx.toString() //2/3
Heres a JSbin as well

Your floating point numbers are approximations of the rational numbers you hope for. See e.g. Is floating point math broken? for details on this. The upshoot is: you can't hope to actually find a numerator and denominator which represent your original fraction.
If you want that fraction, you should have a look at continued fractions. Each truncated continued fraction will represent the best possible rational approximation for an arbitrary value. You can continue this until the error is sufficiently small.
Here is a page visualizing this approximation. The text is in German, but the maths should be clear enough. This page is in English but doesn't have as much visualization.

Related

application spits the numbers back at me even though it should return another value

I'm trying to make this front end web application where you provide acres and karats in a prompt in this form e.g. 3.22 and calculates them and give the total back in the chrome JS console
For example, you have 3.22 acres of land and another land that is 2.2 acres. If you get the sum of these numbers it should give you 5.42, no I want them to return 6, because acres have 24 karats and if you calculate 3 acres and 22 karats + 2 acres and 2 karats it should give you 6 acres, that's what I'm trying make here. I've been trying all night and every time the numbers I put in the prompt gets spit back at me in the console, so here's my code:
window.setTimeout(function() {
var acres = [];
var floats = [];
var wholes = [];
var input = prompt("What would you like to do?");
while (input !== "quit") {
if (input === "total") {
console.log("***********");
acres.forEach(function(total, i) {
console.log(i + ": " + total);
})
console.log("***********");
} else if (input === "calc") {
var num = prompt("Please enter a number");
while (num !== "back") {
if (num === "back") {
break;
}
acres.push(num);
var ftotal = 0
var wtotal = 0;
floats = [];
wholes = [];
for(var i = 0; i < acres.length; i++) {
alert("entered the for loop");
var acresNum = acres.pop();
var str = acresNum.toString();
var number = Math.floor((str).split(".")[1]);
floats.push(number);
ftotal += floats[i];
//-------------------------
var num2 = Math.floor(acresNum);
wholes.push(num2);
wtotal += wholes[i];
}
alert("exited the for loop");
console.log(ftotal);
console.log(wtotal);
if (ftotal > 23) {
wtotal++;
}
acres.push(wtotal + "." + ftotal);
var num = prompt("Please enter a number");
}
}
var input = prompt("What would you like to do?");
}
console.log("OK, YOU QUIT THE APP");}, 500)
The whole logic in this application is in that for loop in the else if(input === "calc") area.
You could take a numerical approach, but you went into the trap of floating point arithmetic (Is floating point math broken?) and get a number which does not match the given value of 42.
function sum(a, b) {
var s = a + b,
i = Math.floor(s),
p = (s - i) * 100;
console.log(p);
if (p >= 42) { // never reached
p -= 42;
++i;
}
return i + p / 100;
}
console.log(sum(3.22, 2.2));
As solution, you could separate the places as a string and add integer values and check if the value is greater than one acre, then return an adjusted value.
function sumD(a, b, threshold) {
return [a, b]
.map(v => v.toString().split('.'))
.reduce((r, a) => {
a.forEach((v, i) => r[i] += +v);
r[0] += Math.floor(r[1] / threshold);
r[1] %= threshold;
return r;
}, [0, 0])
.join('.');
}
console.log(sumD(3.22, 2.2, 24));
Separate the decimal values from your numbers.(Already done)
Ex: 3.22 -> 0.22 and 2.2 -> 0.2
Add them -> 0.22 + 0.2
Divide them by 0.24 -> (0.22+0.2)/.24 = 1
Add that to the wtotal -> 3.00 + 2.00 = 5 -> 5 + 1
I think this should be the logic mathematically.

How can I make power method?

I want make the power method
I made this :
var x = 2, n = 3, i;
for (i = 1; i < n; i++) {
x = x * x;
}
console.log(x);
This gives 16 as result but expected is x^n = 8.
This function doesn't compute the power because it squares the intermediate results. You should use a separate variable like this:
var x= 2 ,n= 3, i;
var y = x;
for(i=1;i<n;i++){
x *= y;
}
console.log(x);
Try recursion:
const power = ( base, exponent ) => {
if( exponent === 0 ) return 1;
else return base * power( base, exponent - 1 );
};
Or try the normal for loop:
const power = ( base, exponent ) => {
let result = 1;
for( let i = 0; i < exponent; i++ )
result *= base;
return result;
};
The reason yours isn't working is because it tries to compute x = x^2 for n steps. Hence, the calculation is 2^2 = 4^2 = 16. The above code, instead, has a result variable which multiplies the base an exponent number of times.
You can use the built-in method Math.pow(number, power).
console.log(Math.pow(2, 10));

Converting a Decimal to a Fraction

I'm working on a "toy problem" where I am supposed to write a JavaScript function that converts a decimal into a fraction and returns it as a string. For example: fractionConverter(2.75) should return "11/4".
Here is my code:
function fractionConverter (number) {
if (number > 0) {
var isNegative = false;
} else if (number < 0) {
var isNegative = true;
}
number = Math.abs(number);
if (number % 1 === 0) {
var finalFrac = number + "/1";
} else {
for (var i = 2; i < 10000000000; i++) {
if ((i * number) % 1 === 0) {
var finalFrac = (i * number) + "/" + i;
}
if (finalFrac) { break; }
}
}
var getFrac = function(numString, bool) {
if (!bool) {
return numString;
} else {
return "-" + numString;
}
}
return getFrac(finalFrac, isNegative);
}
Sorry about the formatting. Anyway, I'm getting a weird spec failure. The function returns the correct values for the following numbers: 0.5, 3, 2.5, 2.75, -1.75 and .88. For some reason, however, it is failing on 0.253213. It is returning 1266065/5000000 instead of 253213/1000000. Not really sure why.
Thanks
I am just improving #william's answer,
I think this script gives you more reduced fraction.
function fractionConverter(number) {
var fraction = number - Math.floor(number);
var precision = Math.pow(10, /\d*$/.exec(new String(number))[0].length);
var getGreatestCommonDivisor = function(fraction, precision) {
if (!precision)
return fraction;
return getGreatestCommonDivisor(precision, fraction % precision);
}
var greatestCommonDivisor = getGreatestCommonDivisor(Math.round(fraction * precision), precision);
var denominator = precision / getGreatestCommonDivisor(Math.round(fraction * precision), precision);
var numerator = Math.round(fraction * precision) / greatestCommonDivisor;
function reduce (numer,denom) {
for (var i = 2; i >= 9; i++) {
if ((numer%i===0) && (denom%i)===0) {
numerator=numer/i;
denominator=denom/i;
reduce(numerator,denominator);
};
};
}
reduce(numerator,denominator);
return numerator + "/" + denominator;
}
document.getElementById("output").innerHTML = fractionConverter(0.24888);
Here is the HTML
<body>
<p id="output"></p>
</body>
</html>
Javascript doesn't deal with floating point numbers accurately.
I tried typing this into node:
0.253213 * 1000000
And I got this:
253213.00000000003
Here is a different approach to testing for a multiplier
var bigNumber = Math.pow(10,8);
var isDivisible = (Math.round(i * number * bigNumber)/bigNumber % 1) == 0;
This will help you some of the way.
This also work the way you might expect it to, if you wanted 0.333333333 to be treated as 1/3.
One issue is that the highest integer you can have is javascript is between 10^15 and 10^16.
If ((number * bigNumber) > 2^53) this will not work.
The caveat to this answer is that ECMAscript inadequately handles Decimals.
Also, note that the following is largely pseudocode, but should work with minor fixes.
Here is a javascript solution to this problem:
var decimal_to_fraction = {
"numerator": 0,
"denominator": 0,
"simplified_numerator": this.numerator,
"simplified_denominator": this.denominator,
"init": function(numerator, denominator){
this.numerator = numerator
this.denominator = denominator
},
"get_divisor": function(numerator, denominator){
var divisor = 0;
var divisors = [1, 2, 3, 4, 5];
for (i in divisors) {
if (!(numerator % divisor) && !(denominator % divisor)) {
divisor = i;
break
}
}
return divisor
},
"calculate_fraction": function() {
var simplified = false;
divisor = this.get_divisor(numerator_denominator);
if (divisor) {
while (simplified == false) {
if (this.simplfieid_numerator / divisor and this.simplified_denominator / divisor) {
this.simplified_numerator = simplified_numerator / divisor
this.simplified_denominator = simplified_denominator / divisor
} else {
simplified = true
}
}
}
return (this.simplified_numerator, this.simplfieid_denominator)
},
"get_fraction": function() {
this.calculate_fraction()
fraction = "{0} / {1}".format(this.simplfieid_numerator, this.simplified_denominator"
return fraction
}
}
decimal_to_fraction.get_fraction()
In case you were curious, here's a Python solution to your problem:
class DecimalToFraction(object):
def __init__(decimal):
self.numerator = decimal * 100
self.denominator = 100
self.simplified_numerator = self.numerator
self.simplified_denominator = self.denominator
def get_divisor(self, numerator, denominator):
divisor = 0
for i in range(0,5):
if not numerator % divisor and not denominator % divisor:
divisor = i
break
return divisor
def calculate_fraction(self):
simplified = False
divisor = get_divisor(self.numerator, self.denominator)
if divisor:
while simplified == False:
if self.simplified_numerator / divisor and self.simplfieid_denominator / divisor:
self.simplified_numerator = simplified_numerator / divisor
self.simplified_denominator = simplified_denominator / divisor
else:
simplified = True
return (self.simplified_numerator, self.simplified_denominator)
def get_fraction(self):
self.calculate_fraction()
fraction = "{0} / {1}".format(self.simplified_numerator, self.simplified_denominator)
return fraction
#d2f = DecimalToFraction(<decimal>)
#d2f.get_fraction()
I completely changed the structure of your code, but this solution does work. It is based off of code from this thread. I hope this helps.
function fractionConverter(number) {
var fraction = number - Math.floor(number);
var precision = Math.pow(10, /\d*$/.exec(new String(number))[0].length);
var getGreatestCommonDivisor = function(fraction, precision) {
if (!precision)
return fraction;
return getGreatestCommonDivisor(precision, fraction % precision);
}
var greatestCommonDivisor = getGreatestCommonDivisor(Math.round(fraction * precision), precision);
var denominator = precision / greatestCommonDivisor;
var numerator = Math.round(fraction * precision) / greatestCommonDivisor;
return numerator + "/" + denominator;
}
document.getElementById("output").innerHTML = fractionConverter(0.253213);
<!DOCTYPE html>
<html>
<body>
<p id="output"></p>
</body>
</html>
You can use Erik Garrison's fraction.js library to do that and more fractional operations.
To to do 1.75 , you can just do
var f = new Fraction(1.75);
console.log(f.toFraction()); // Results "1 3/4"
console.log(f.s * f.n + " / " + f.d); // Results "7 / 4"
console.log(f.toString()); // Results "1.75

convert decimal number to fraction in javascript or closest fraction [duplicate]

This question already has answers here:
How to simplify a decimal into the smallest possible fraction?
(6 answers)
Closed last year.
The community reviewed whether to reopen this question last year and left it closed:
Original close reason(s) were not resolved
So i want to be able to convert any decimal number into fraction. In both forms such as one without remainder like this: 3/5 or with remainder: 3 1/4.
what i was doing is this..
lets say i have number .3435.
Calculate amount of digits after decimals.
multiply by 10 with power of the amount before number.
then somehow find greatest common factor.
Now i don't know how to find GCF. And nor i know how to implement logic to find fraction that represents a number closely or in remainder form if exact fraction doesn't exists.
code i have so far: (testing)
x = 34/35;
a = x - x.toFixed();
tens = (10).pow(a.toString().length - 2);
numerator = tens * x;
denominator = tens;
Your first 2 steps are reasonable.
But what you should do is for the numerator and denominator calculate the Greatest Common Divisor (GCD) and then divide the numerator and denominator with that divisor to get the fraction you want.
GCD is rather easy to calculate. Here is Euclid's algorithm:
var gcd = function(a, b) {
if (!b) return a;
return gcd(b, a % b);
};
Edit
I've added a fully working JSFiddle.
Unless you are willing to work on developing something yourself then I would suggest using a library that someone has already put effort into, like fraction.js
Javascript
var frac = new Fraction(0.3435);
console.log(frac.toString());
Output
687/2000
On jsFiddle
You can use brute force test on different denominators and retain the result that has least error.
The algorithm below is an example of how you might go about this, but, suffers from being inefficient and limited to searching for denominators up to 10000.
function find_rational( value, maxdenom ) {
console.clear();
console.log( "Looking up: " + value );
let best = { numerator: 1, denominator: 1, error: Math.abs(value - 1) }
if ( !maxdenom ) maxdenom = 10000;
for ( let denominator = 1; best.error > 0 && denominator <= maxdenom; denominator++ ) {
let numerator = Math.round( value * denominator );
let error = Math.abs( value - numerator / denominator );
if ( error >= best.error ) continue;
best.numerator = numerator;
best.denominator = denominator;
best.error = error;
console.log( "Intermediate result: "
+ best.numerator + "/" + best.denominator
+ " (" + ( best.numerator/best.denominator)
+ " error " + best.error + " )" );
}
console.log( "Final result: " + JSON.stringify( best ) );
return best;
}
function calc() {
const value = parseFloat( $("#myInput").val() );
if ( isNaN(value) ) {
$( "#myResult" ).val( "NaN" );
return;
}
const rational = find_rational( value, 10000 );
$("#myResult").val( rational.numerator
+ " / " + rational.denominator
+ " ( Error: " + rational.error + " )" );
}
calc();
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<P>
Enter a decimal number:<BR/>
<INPUT type="text" name="myInput" id="myInput" value=".3435" onkeyup="calc()"/><BR/>
</P>
<P>
Resulting Rational:<BR/>
<INPUT name="myResult" id="myResult" value=""/><BR/>
</P>
The above determines the .3435 as a fraction is 687 / 2000.
Also, had you gave it PI (e.g. 3.1415926) it produces good looking fractions like 22/7 and 355/113.
One quick and easy way of doing it is
getFraction = (decimal) => {
for(var denominator = 1; (decimal * denominator) % 1 !== 0; denominator++);
return {numerator: decimal * denominator, denominator: denominator};
}
I get very poor results using the GCD approach. I got much better results using an iterative approach.
For example, here is a very crude approach that zeros in on a fraction from a decimal:
function toFraction(x, tolerance) {
if (x == 0) return [0, 1];
if (x < 0) x = -x;
if (!tolerance) tolerance = 0.0001;
var num = 1, den = 1;
function iterate() {
var R = num/den;
if (Math.abs((R-x)/x) < tolerance) return;
if (R < x) num++;
else den++;
iterate();
}
iterate();
return [num, den];
}
The idea is you increment the numerator if you are below the value, and increment the denominator if you are above the value.
Use the Euclidean algorithm to find the greatest common divisor.
function reduce(numerator,denominator){
var gcd = function gcd(a,b){
return b ? gcd(b, a%b) : a;
};
gcd = gcd(numerator,denominator);
return [numerator/gcd, denominator/gcd];
}
This will provide you with the following results on your console
reduce(2,4);
// [1,2]
reduce(13427,3413358);
// [463,117702]
So by continuing from already what you have,
var x = 34/35;
var a = x - x.toFixed();
var tens = Math.pow(10,a.toString().length - 2);
var numerator = tens * x;
var denominator = tens;
reduce(numerator,denominator);
Source: https://stackoverflow.com/a/4652513/1998725
I had researched all over the website and I did combine all code into one, Here you go!
function fra_to_dec(num){
var test=(String(num).split('.')[1] || []).length;
var num=(num*(10**Number(test)))
var den=(10**Number(test))
function reduce(numerator,denominator){
var gcd = function gcd(a,b) {
return b ? gcd(b, a%b) : a;
};
gcd = gcd(numerator,denominator);
return [numerator/gcd, denominator/gcd];
}
return (reduce(num,den)[0]+"/"+reduce(num,den)[1])
}
This code is very easy to use! You can even put number in this function!
The tricky bit is not letting floating points get carried away.
Converting a number to a string restrains the trailing digits,
especially when you have a decimal with an integer, like 1.0625.
You can round off clumsy fractions, by passing a precision parameter.
Often you want to force a rounded value up, so a third parameter can specify that.
(e.g.; If you are using a precision of 1/64, the smallest return for a non-zero number will be 1/64, and not 0.)
Math.gcd= function(a, b){
if(b) return Math.gcd(b, a%b);
return Math.abs(a);
}
Math.fraction= function(n, prec, up){
var s= String(n),
p= s.indexOf('.');
if(p== -1) return s;
var i= Math.floor(n) || '',
dec= s.substring(p),
m= prec || Math.pow(10, dec.length-1),
num= up=== 1? Math.ceil(dec*m): Math.round(dec*m),
den= m,
g= Math.gcd(num, den);
if(den/g==1) return String(i+(num/g));
if(i) i= i+' and ';
return i+ String(num/g)+'/'+String(den/g);
}
Math.roundFraction(.3435,64); value: (String) 11/32
Inspired by #chowey answer, which contained recursive implementation of finding close fraction for a decimal value within given tolerance, here is better (see benchmark), iterative version of it.
function toFractionIterative(x, epsilon = 0.0001) {
if (x == 0) return [0, 1];
const a = Math.abs(x);
let n = 0;
let d = 1;
let r;
while (true) {
r = n / d;
if (Math.abs((r - a) / a) < epsilon) {
break;
}
if (r < a) {
n++;
}
else {
d++;
}
}
return [x < 0 ? -n : n, d];
}
Benchmark (tl;dr: recursive 1,589 ops/s, iterative 5,955 ops/s; use iterative approach)
let v = 3.141592;
document.write(d2f(v)); // 392699/125000
function d2f(v) // decimal to fraction
{
if (Math.floor(v) == v) return v + '/' + 1;
v = Math.abs(v);
let ret = .01, // rounding error tolerance
td = v-Math.floor(v), // trailing digits
r = 1/td, // reciprocal
d = r, // start building denominator
lim = 20; // max loop limit
for (let i = 0; i < lim; i++)
{
td = r-Math.floor(r);
if (Math.abs(r-Math.round(r)) < ret) break;
r = 1/td;
d *= r;
}
return Math.round(d*v) + '/' + Math.round(d);
}
I came up with this for 16ths
function getfract(theNum){
var input=theNum.toString();
var whole = input.split(".")[0];
var rem = input.split(".")[1] * .1;
return(whole + " " + Math.round(rem * 16) + "/16");
}
function decimalToFraction(num) {
let numsAfterDecPoint = num.toString().split('.')[1] ? num.toString().split('.')[1].length : 0;
let numerator = num * Math.pow(10, numsAfterDecPoint);
let denominator = Math.pow(10, numsAfterDecPoint);
console.log(numerator + " / " + denominator)
let d = GCD(numerator,denominator)
return numerator / d + " / " + denominator / d
}
console.log(decimalToFraction(0.5)); // 5 / 10 => 1 / 2
console.log(decimalToFraction(178.45)); // 17845 / 100 => 3569 / 20
function GCD(a,b) {
let r = 0;
while(b != 0) {
r = a % b
a = b;
b = r;
}
return a;
}

Reverse decimal digits in javascript

How do I reverse the digits of a number using bitwise?
input:
x = 123;
output:
x = 321;
How Do this?
That's not inverting bits; that's reversing the order of decimal digits, which is completely different. Here's one way:
var x = 123;
var y = 0;
for(; x; x = Math.floor(x / 10)) {
y *= 10;
y += x % 10;
}
x = y;
If you actually want to invert bits, it's:
x = ~x;
As a function:
function reverse(n) {
for(var r = 0; n; n = Math.floor(n / 10)) {
r *= 10;
r += n % 10;
}
return r;
}
If you wanted to make a simple reversal:
var x = 123;
var y = x.toString();
var z = y.split("").reverse().join("");
var aa = Number(z);
document.write(aa);
http://jsfiddle.net/jasongennaro/gV39e/
Here is another way...
var reversed = num.toString().split('').reverse().join('');
jsFiddle.
If you wanted it again as a Number, use parseInt(reversed, 10). Keep in mind though, leading 0s are not significant in a decimal number, and you will lose them if you convert to Number.
you also use this function
function myfunction(a){
var x=a.toString();
var y= x.split("");
var z=y.reverse();
var result=z.join("");
return result;
}
myfunction(123);
Simple and quick solution: Let's assume that you want to reverse a number 4546. You will take the reminder from each division by 10 and append it to the result until the number is > 0. And simultaneously updating the num variable by dividing it by 10.
var x = '';
var num = 4546;
while(num>0){
x = x + (num%10);
num = parseInt(num/10);
}
console.log(x);
Reversing The Positive/ Negative Integer Number
function reverseInt(n) {
return parseInt(n.toString().split('').reverse().join()) * Math.sign(n)
}
If n is -5, then Math.sign(n)==> will return -1
If n is 5, then Math.sign(n)==> will return 1
Here are reversible array functions in JavaScript that handle integers or strings:
function reverse(array)
{
var left = null;
var right = null;
var length = array.length;
for (left = 0, right = length - 1; left < right; left += 1, right -= 1)
{
var temporary = array[left];
array[left] = array[right];
array[right] = temporary;
}
return array;
}
function toDigitsArrayFromInteger(integer, isReverse)
{
var digits = [];
if (integer > 0)
{
var floor = window.Math.floor;
while (integer > 0)
{
digits.push(floor(integer % 10));
integer = floor(integer / 10);
}
// Array is populated in reverse order. Un-reverse it to make it normal.
if (!isReverse)
{
digits = reverse(digits);
}
}
else if (integer < 0)
{
digits = toDigitsArrayFromInteger(-integer, isReverse);
}
else if (integer === 0)
{
digits.push(0);
}
return digits;
}
function toDigitsArrayFromString(string, isReverse)
{
var digits = [];
string += ""; // Coerce to string.
var i = null;
var length = string.length;
for (i = 0; i < length; i += 1)
{
var integer = parseInt(string.charAt(i), 10);
if (isFinite(integer))
{
digits.push(integer);
}
}
if (isReverse)
{
digits = reverse(digits);
}
return digits;
}
Once you have the digits as an array, you can reverse the array easily to get the digits starting from the left or from the right.
The string function is more versatile because it can find any digit in a string, whereas the integer function is limited to integers.
Benchmarks:
http://jsperf.com/todigitsarray
The benchmarks between the two functions show that in Firefox 10 and Chrome 12, the string function is 30% to 60% faster than the integer function. In Opera 12, the integer function is slightly faster by about 10%.
//reverse integer
const revInt = (num)=>{
//turn into string
if(Math.sign(num)===1)
return parseInt(num.toString().split('').reverse().join(''));
else return -1*parseInt(num.toString().split('').reverse().join(''));
}
console.log(revInt(-501));
<html>
<script>
function reverseInt(n){
var r=0;
while(n!=0){
r*=10;
r+=n%10;
n=Math.floor(n/10);
}
return r;
}
</script>
</html>
try this
var n = 352;
function loop(n, r){
if(!n) return r;
r = (r ? r * 10 : 0) + n % 10;
return loop(Math.floor( n / 10), r);
}
console.log(loop(n));
OK, how about using and chaining these popular tricks in JavaScript in one-line function as below...
const reverseNum = num => +("" + ~~num.split("").reverse().join(""));
And call it like these:
reverseNum(123); //321
reverseNum(423.09); //324
reverseNum(23305.1); //50332
reverseNum(89112); //21198
reverseNum(568434.2389); //434865
This takes Number x as a parameter and returns the reversed number.
const reverse = (x) => Number(x.toString().split("").reverse().join(""));
Memory Usage: 35.3 MB, less than 100.00% of JavaScript online submissions for Reverse Integer on leetcode.com.
Runtime: 80 ms, faster than 61.48% of JavaScript online submissions for Reverse Integer.
Time complexity is O(log10(n)).
function reverse(x) {
let rev = 0;
const isNegative = Math.sign(x) === -1;
const isOverflow = n => n > 2**31;
x = Math.abs(x);
while (x) {
let pop = x % 10;
x = Math.floor(x / 10);
rev = rev * 10 + pop;
if (isOverflow(rev)) {
return 0;
}
}
return isNegative ? rev * -1 : rev;
}
The code block below should do the trick
<script type = "text/javascript">
var input;
input=window.prompt ("Please enter a number to be reversed.");
x=input.length;
while(x > 0)
{
x=x-1;
document.write(input[x]);
}
</script>

Categories

Resources