Skip to content

Tag: numbers

JavaScript factorial prevent infinity

I have been using this function for calculating factorial numbers in JavaScript: All seemed to be going well until I tried the number 500. It returned infinity. Is there a way that I can prevent infinity as an answer? Thank you. Answer You indeed need to use bignumbers. With math.js you can do: This will outp…

How do I convert an integer to binary in JavaScript?

I’d like to see integers, positive or negative, in binary. Rather like this question, but for JavaScript. Answer A solution i’d go with that’s fine for 32-bits, is the code the end of this answer, which is from developer.mozilla.org(MDN), but with some lines added for A)formatting and B)checking t…

JavaScript numbers to Words

I’m trying to convert numbers into english words, for example 1234 would become: “one thousand two hundred thirty four”. My Tactic goes like this: Separate the digits to three and put them on Array (finlOutPut), from right to left. Convert each group (each cell in the finlOutPut array) of th…

Converting hexadecimal to float in JavaScript

I would like to convert a number in base 10 with fraction to a number in base 16. All is well there. Now I want to convert it back to decimal. But now I cannot write: As it doesn’t return the decimal part. And I cannot use parseFloat, since per MDC, the syntax of parseFloat is It wouldn’t have bee…

Comparing negative numbers in javascript

I’m sure this is a simple problem, but I’m comparing negative numbers in javascript i.e.: This script will always take action 2, even though num1 is less than num2. Whats going on here? Answer How does if (parseFloat(num1) < parseFloat(num2)) work? Maybe your numbers are turning into strings so…