Getting a NaN error, can't figure out why - javascript

I have this code in JavaScript:
SolarPanels = parseInt(lRemainingWidth / (panel_width + ( 2 * lPanelInterSpace)));
and then I alert the SolarPanels value which gives NaN as output,
alert("SolarPanels "+SolarPanels);
This 1 line is a tiny part of a huge calculation, but my code seems to fail here,
with the use of alerts i've read out the values of SolarPanels, lRemainingWidth, panel_width and lPanelInterSpace
which are the following:
lRemainingwidth = 17.4.320227272727276
SolarPanels = 0
panel_width = 1.65
lPanelInterSpace = 0.02
I think it has to do with the 2 dots in lRemainingWidth, either way, I don't know how to fix it. Why the lRemainingWidth has 2 dots?
Update :
This is the part that calculates the lRemainingWidth :
if(HalforDouble === "Yes")
{
lRemainingWidth = (roof_ridge /2) + ((lRemainingHeight / Math.tan((lRoofEdgeDegrees * Math.PI) / 180)) - lRoofEdge);
}
else
{
lRemainingWidth = roof_ridge + (2 * ((lRemainingHeight / Math.tan((lRoofEdgeDegrees * Math.PI) / 180)) - lRoofEdge));
}
The values here are:
lRemainingWidth = 0
roof_ridge = 17
lRemainingHeight = 20.769000000000002
lRoofEdgeDegrees = 83.5169263071276
lRoofEdge = 0.2

Your problem is that you mix strings and numbers
Start with this code before any computation :
var roof_ridge = parseFloat(roof_ridge);
There might be other strings hidden in your code but we don't see them. Apply the same conversion on them.

lRemainingWidth = roof_ridge + (2 * ((lRemainingHeight / Math.tan((lRoofEdgeDegrees * Math.PI) / 180)) - lRoofEdge));
If roof_ridge is a string then the + does string concatenation instead of addition.
Change this to
lRemainingWidth = +roof_ridge + (2 * ((lRemainingHeight / Math.tan((lRoofEdgeDegrees * Math.PI) / 180)) - lRoofEdge));
The prefix + operator in +roof_ridge coerces its argument to a number.

It seems like a cornerstone of the issue in roof_ridge variable. This variable is instance of String class, not a number. So, when you code go to this line:
lRemainingWidth = roof_ridge + (2 * ((lRemainingHeight / Math.tan((lRoofEdgeDegrees * Math.PI) / 180)) - lRoofEdge));
the next calculation is done:
'17' + whatever_float_value = got string concatenation instead of number's sum.
To fix this just add:
lRemainingWidth = parseFloat(roof_ridge) + (2 * ((lRemainingHeight / Math.tan((lRoofEdgeDegrees * Math.PI) / 180)) - lRoofEdge));

Related

Javascript Low-pass filter

How to properly apply a low pass filter in javascript? I'm not sure if my doing is correct or not. And I can't find a good example in javascript language
I tried using this but it returns NaN
// Use a basic low-pass filter to keep only the gravity component of each axis.
var grav_accelX = (acceleration.x * kFilteringFactor) + ( grav_accelX * (1.0 - kFilteringFactor));
var grav_accelY = (acceleration.y * kFilteringFactor) + ( grav_accelY * (1.0 - kFilteringFactor));
var grav_accelZ = (acceleration.z * kFilteringFactor) + ( grav_accelZ * (1.0 - kFilteringFactor));
So what I did is like this. It gives value but I'm not sure if this is the proper way.
const kFilteringFactor = 0.9
var accelX = x * kFilteringFactor
var accelY = y * kFilteringFactor
var accelZ = z * kFilteringFactor
accelX = accelX * (1.0 - kFilteringFactor)
accelY = accelY * (1.0 - kFilteringFactor)
accelZ = accelZ * (1.0 - kFilteringFactor)
The inline grav_accelX variable seems like not working here's the example

javascript - keep getting error with multiple equation with words and results

I am trying to get a result that looks like this
"The volume of the cone 261.66 is greater than the volume of the cylinder 785: false."
console.log("The volume of the cone " +(10 / 3 * (3.14 * (5 **2))) "is greater than the volume of the cylinder " +(10 * (3.14 * (5 **2)))): 261 > 785);
EDIT: Thanks for all your help everyone. I can believe I didn't see that I didn't put + and thanks for point out ":" I was so tired and I just wanted to finish I can't believe I wasted all that time getting the math to work just to miss a + and forgot about :
You missed adding + or concatenation after the numerical expressions and : should have been in quotes too as its a string. Better yet use templating, both examples below:
// With concatination
console.log("The volume of the cone " + (10 / 3 * (3.14 * (5 ** 2))) + " is greater than the volume of the cylinder " + (10 * (3.14 * (5 ** 2))) + ": " + (261 > 785));
// With templating
console.log(`The volume of the cone ${(10 / 3 * (3.14 * (5 ** 2)))} is greater than the volume of the cylinder ${(10 * (3.14 * (5 ** 2)))}: ${(261 > 785)}`);
You missed a + and ":"
With template literals
console.log(`The volume of the cone ${(10 / 3 * (3.14 * (5 **2)))} is greater than the volume of the cylinder ${(10 * (3.14 * (5 **2)))} : ${261 > 785}`);
Template literals
It should be one from below
String Concatenation
console.log("The volume of the cone " + (10 / 3 * (3.14 * (5 **2))) + "is greater than the volume of the cylinder " + (10 * (3.14 * (5 **2))) + ":" + (261 > 785));
Please Note the importance of the paranthesis in (261 > 785). Removing the bracket will throw a false as output. Because the string expression in console will be checked against > 785 if the bracket is removed, that will result in false as result
OR
Template Literals
console.log(`The volume of the cone ${(10 / 3 * (3.14 * (5 **2)))} is greater than the volume of the cylinder ${(10 * (3.14 * (5 **2)))} : ${261 > 785}`);

How to use Math.float for a damage formula equation

I have found a guide for some damage formulas from FF9 and want to use them
in RMMV which uses Javascript.
Im bad at math and dont know how the Math.random and modulo
works or at least im doing something wrong.
I allways get 0 damage and im sure its because of wrong equation.
Since the Math.random gives a float from 0.00 to 1.00 I thought that would be the problem.
So I've tried to use a random number between 1 and 100 but that didnt helped.
Base = Spell Power - Mag Def
Bonus = Mag + Rnd MOD ([(Lvl + Mag) / 8] + 1)
Damage = Base * Bonus
SPELLPOWER - b.mdf * (a.mat + (Math.random() % ((a.level + a.mat) / 8) + 1))
16 - 2 * (16 + (Math.random() % ((1 + 16) / 8) + 1))
SPELLPOWER - b.mdf * (a.mat + ((Math.random() * (100 - 1) + 1) % ((a.level + a.mat) / 8) + 1))
16 - 2 * (16 + ((Math.random() * (100 - 1) + 1) % ((1 + 16) / 8) + 1))
Somehow this should actualy give a number higher then 0 with the stats i provide.
It was just some wrong set brackets which caused the trouble.
(SPELLPOWER - b.mdf) * (a.mat + Math.random() % (((a.level + a.mat) / 8) + 1))
This works perfectly fine now.

How to round down number 2 decimal places? [duplicate]

This question already has answers here:
How to round to at most 2 decimal places, if necessary
(91 answers)
Closed 6 years ago.
How to get following inputs to bellow outputs
Input
1.0789
10.350
1.7777
Output
1.07
10.35
1.77
Use Math.floor to round the decimal places under the current value.
Reference
Example
Math.floor(1.0789 * 100) / 100
Working Fiddle
console.log(Math.floor(1.0789 * 100) / 100);
console.log(Math.floor(10.350 * 100) / 100);
console.log(Math.floor(1.7777 * 100) / 100);
console.log(Math.floor(12.34 * 100) / 100);
you have several methods for do this
Use Math.round(num * 100) / 100
Use Math.ceil(num * 100)/100;

Car payment estimation formula in javascript

The Formula looks like this:
(25000 x (.06 / 12)) / (1 - ((1 + (.06 / 12))^(-36))) = 760.548436
I have been attempting to convert this to javascript but with not much luck. If you put that above formula into google you will see the answer.
After many attempts and different methods, braking up the formula into different variables then dividing them, I haven't had any luck, when I came up with this, it got me the wrong answer:
var loan = 25000;
var rate = 6 / 100;
var term = 36;
var calculate = (loan * (rate / 12)) / (1 - ((1 + (rate / 12))^(-term)));
console.log(calculate);
Output was:
3.4722222222222223
And not 760.548436. Anyone have any ideas?
The caret is a bitwise operator. You want this formula instead:
calculate = (loan * (rate / 12)) / (1 - Math.pow(1 + (rate / 12), -term));
It gives you the answer you expect, 760.5484362888927

Categories

Resources