This question already has answers here:
Number with leading zeroes gets changed in JavaScript
(4 answers)
Why JavaScript treats a number as octal if it has a leading zero
(3 answers)
Closed 1 year ago.
solving Sum of Digits / Digital Root and facing problem in this case where input is 010 the output comes 8.
please can anyone explain?
function fun(n) {
let numString = n.toString().split("");
let sum = 0;
for (let i = 0; i < numString.length; i++) {
sum += parseInt(numString[i]);
}
console.log(sum);
if (sum >= 10) {
return fun(sum);
}
console.log(numString);
return sum;
}
console.log(fun(010)); // input- 010 and output- 8
Why the output is 8 but it should be 1.
This question already has answers here:
Truncate number to two decimal places without rounding
(43 answers)
Closed 2 years ago.
I got a question.
My backend recieve number string from frontend.
The important point is, I want to cut number at the second demical point.
ex1) '2.346' => 2.34
ex2) '2.3' => 2.3
ex3) '4.246' => 4.24
ex4) '4.1' => 4.1
And when I try this code with '2.346' or '4.246'
let v = '2.346'
v = parseInt(v * 100) / 100
console.log(v)
// v = 2.34
But when I try this code with 2.3 or 4.1, it makes wierd...
let v = '2.3'
v = parseInt(v * 100) / 100
console.log(v)
// v = 2.29
what is the problem in my code...?
Floating-point precision means that multiplying and then dividing by the same number like with your parseInt(v * 100) / 100 may sometimes have long trailing insignificant digits that weren't there to begin with.
If I were you, I'd use a regular expression to match up to 2 digits past a . instead:
const clean = str => str.match(/\d+(?:\.\d{1,2})?/)[0];
console.log(clean('2.346'));
console.log(clean('2.3'));
function tofixed(str){
return parseInt(str * 1000 / 10) /100
}
console.log(tofixed("2.346"))
console.log(tofixed("2.3"))
console.log(tofixed("4.246"))
console.log(tofixed("4.1"))
Im trying to figure out how to write a JavaScript program that computes and outputs the volume of a regular tetrahedron. This is how far I got but it seems to get a error and not compute the right numbers after.
The equation for the triangle is
v = a3
6 √ 2
Sorry about the code i dont know how to post things on here very effectively. So this is my variables
var a = parseFloat(document.getElementById('length').value);
var b = (a * a * a) / 6 * Math.sqrt(2)
You are very close. You are missing some parenthesis around 6 * Math.sqrt(2)
Your code is doing (a*a*a) / 6 and then multiplying that result by the square root of 2.
You can read up on Operator Precedence
var a = 4;
var b = (a * a * a) / (6 * Math.sqrt(2))
console.log(b);
You can also use Math.pow()
var a = 4;
var b = Math.pow(a,3) / (6 * Math.sqrt(2))
console.log(b);
This question already has answers here:
toFixed function not working properly ( please give a reason not an alternative)
(3 answers)
Why are floating point numbers inaccurate?
(5 answers)
Closed 5 years ago.
In this example/behaviour is something very strange.
Why does the function toFixed for the first two examples work and for this last one not?
//example 1
var num = 554.956;
var n = num.toFixed(2)
console.log(n);
var num2 = 554.955;
var n2 = num2.toFixed(2)
console.log(n2);
//output 554.96 and 554.96
//example 2
var num5 = 5.956;
var n5 = num5.toFixed(2)
console.log(n5);
var num6 = 5.955;
var n6 = num6.toFixed(2)
console.log(n6);
//output 5.96 and 5.96
//example 3
var num3 = 55.956;
var n3 = num3.toFixed(2)
console.log(n3);
var num4 = 55.955;
var n4 = num4.toFixed(2)
console.log(n4);
//output 55.96 and 55.95
Related to: php round vs javascript toFixed
EDIT: about the duplicates especially this one: toFixed function not working properly ( please give a reason not an alternative)
The answer is very good and helps me to understand the difference between x.955 and x.956, but doesn't answer why this happens only to the 55.955 in my example and not to the 5.955 or 554.955.
This question already has answers here:
Calculating pow(a,b) mod n
(14 answers)
Closed 6 years ago.
Is there are trick to get the modulo of big numbers in Javascript. I am getting infinity with modulo(7, 16971, 25777) 7^16971mod25777=NaN
function modulo (n, p, m){
var x = Math.pow(n, p);
var y = m;
var z = x%y;
alert(x);
return z;
}
There's a mathematical "trick" you can use, if you can assume all parameters are integers.
Consider the following modulo operation:
(a*x + y) % x
Obviously, the a*x part can be discarded and the following holds:
(a*x + y) % x = y % x
With that in mind we can assume the big number is just a*x + y, and we can perform the modulo at any stage, and as often as we like, so, to get the result you want, do this:
function modulo (n, p, m){
var result = 1;
while(p--) {
result = (result * n) % m;
}
return result;
}
console.log(modulo(7, 16971, 25777));
JavaScript numbers are stored as 64-bit floats.
Math.pow(7, 16971) is Infinity because the value is too large for that representation. Specifically, it's larger than Number.MAX_VALUE, which is 1.7976931348623157e+308.
The largest safe integer is Math.pow(2, 53) - 1), aka Number.MAX_SAFE_INTEGER.
You can use an arbitrary size integer library like big-integer to work with larger integers:
const result = bigInt(7).modPow(16971, 25777);
console.log(result.value); // 857
JSFiddle
You'll probably want to look into a large number library such as big.js to do this. It has its own mod() function to handle larger numbers and greater floating point precision.
From the manual:
1 % 0.9 // 0.09999999999999998
x = new Big(1)
x.mod(0.9) // '0.1'
Please try this, it should work for you...
<script src="http://peterolson.github.com/BigInteger.js/BigInteger.min.js"></script>
<script>
function modulo(n, p, m) {
var x = bigInt(n).pow(p);
var y = m;
var z = bigInt(x).mod(y);
alert(x);
alert(z);
return z;
}
modulo(7, 16971, 25777);
</script>
Value of X= ( 144157446840451635235083706110907852415749228859252529148906391999766994677256648514596635518338118874745245599504027645569205474259056773767697690363704468632892152795016715055324575445087682781252313005869045568884109150825799944546337893064300709178398146710515468212610079448225972249066488499049225372747076806433631659786194988344294497773759564575000162869574365014937829611100108282508068839769488427218809418476143641444334160948843097387146975458980549194883596975058014553601039150039974922599124812752683319818785474747861041069869797998022819369652619759825244859686407688179575508679861543683676353692931928781365284923967762962761189903683793268647203089135578161089792845634056425105473120490657724974694040110140134504449715061852058159494813855440466218772852172975097582562908895057311050472869260715192269051794091102837753073541384982827121618414372575452344004360364276677087398549812260325448141226947881328515773351976616276417638128022815680053293310617319251468387901625157...56951333749257599033126883342183151178668919812064049965349560466150682525651094508048667165975539000764644172767648163518366194953573817885103167718630743142062623550549541359220427411352708364483389060986844929269143259135008252906461288098421933603373774514126347477000279431329468363160423511545129487503178839098880369937328996412126931687097210220191726087729442555830870326323512951767388505151559227624666317971526350895004302090730198002124799887057180493028281166853990182770936726392403645367304961828645095221020100469965292184204520213166368848723223621651107654075062116217744242552262031457878341343131239324794711518591327361143916482110866686618572491075943511233044928342441933757654662089762470943194596874717623496819342403306038522266428198018364568515908102686200233757394776127456240030822204960242512397946554388855232832783930954979762030089547004776120626513910030444279665047610388454114197939348310563226006027400434616239674784018828580353008938225035036985223336494743), please take a look at output screen below.
Value of Z=(857). please take a look at output screen below.