I have a project I am working on and it needs to calculate mortgage calculations but I'm having trouble putting the formula into javascript.
the formula is:
M = P I(1 + I)^n /(1 + I )^n - 1
Any help is appreciated, thanks
P = loan princible
I = interest
N = Term
Break it down into a sequence of steps.
Multiplication is as straightforward as it gets: I*(1+I)
Division is the same: I/(1+I)
To the power of n is denoted by: Math.pow(3, 5); //3 to the power of 5
Math.pow() might be the only thing you didn't know yet.
Unrelated but useful,
Wrap your formula into a function and you have a mortgage-calculation function
calculateMortgage(p,i,n) {
result = //translate the formula in the way I indicated above
return result;
}
and call it like so:
var mortgage = calculateMortgage(300,3,2); // 'mortgage' variable will now hold the mortgage for L=300, I=3, N=2
Also, the formula you posted really doesn't make any sense - why is there a blank between P & I at the very beginning? Something's missing.
Try this: Math.pow(p*i*(1+i),n)/Math.pow(1+i,n-1)
Math.pow(a,2) is same as a^2
if P is not supposed to be with numerator then
this
p * (Math.pow(i*(1+i),n)/Math.pow(1+i,n-1))
or
p * (Math.pow((i+i*i),n)/Math.pow(1+i,n-1))
var M;
var P;
var I;
M = P*(Math.pow(I*(1+I),n)) / (Math.pow((1+I),n)-1);
Does this look right to you? I got the correctly styled formula from here.
Like what Nicholas above said, you can use functions to make it all the more easier.
var M;
function calculateMortgage(P, I, N){
M = P*(Math.pow(I*(1+I),n)) / (Math.pow((1+I),n)-1);
alert("Your mortgage is" + M);
}
And just call calculateMortgage(100, 100, 100); with your values for it to automatically give the answer to you.
Related
I realize this is more of a math question, but I don't have a math brain. I'm specifically interested in solving this problem in JavaScript, just for this specific case. If someone can do this for me with a simple function, I can generalize the solution myself as needed.
I have two lines on a graph. One is linear, just going straight from (0,0) to (x,x):
// Line 1
var f1 = function(x) {
return x;
};
The other line is curved, and can be drawn like this:
// Line 2
var f2 = function(x) {
var alpha = 0.3;
return (1 - alpha) *
(1.4 *
1.6 ** alpha) *
(x ** -alpha);
};
Given only these functions, can I write a function that gives me the co-ordinates of the point(s) at which these two lines intersect?
I've looked at things like algebra.js, but haven't been able to come up with the solution myself.
Derivating process:
So, for alpha= a= 0.3 we have:
k= (1-alpha) * 1.4 * 1.6**alpha = 1.12839738
x= k ** (1/(1 + alpha)) = 1.09737595
The desired intersection is {x, f(x)} = {1.0974, 1.0974}
I have this block, that pulls the current amount paid on the item from Firebase and the amount being paid at the moment. Then it is supposed to add the two together to make the third variable.
For some reason the code drops the cents off the total.
singleRef.once("value", function(snapshot) {
var a = parseInt(snapshot.val().amounts.paid); // The amount already paid
var b = parseInt(invoice.payment.amount); // The amount being applied
var c = a + b;
console.log(c);
});
Let's say the following is happening:
a = 0;
b = 10.86;
The result in this code will be:
c = 10; // should be 10.86
Let's say the following is happening:
a = 10.00;
b = 10.86;
The result in this code will be:
c = 20; // should be 20.86
It doesn't matter what the cents are, it always rounds to get rid of them. I've tried adding .toFixed('2') to all of the variables, just a and b, and just c. All result in the same no cent totals.
HOW!? I've been trying to do this for the past few hours, it's probably simple but I can't figure it out. It's driving me nuts! I'm using angularjs and firebase.
The function parseInt() is specifically for parsing integers so, if you give it 3.14159, it will give you back 3.
If you want to parse a floating point value, try parseFloat().
For example, the following code:
var a = parseInt("3.141592653589");
var b = parseInt("2.718281828459");
var c = a + b;
alert(c);
var d = parseFloat("3.141592653589");
var e = parseFloat("2.718281828459");
var f = d + e;
alert(f);
will give you two different outputs:
5
5.859874482047999
As others have mentioned, you can't parse dollars and cents with parseInt().
And using floats is a bad idea for anything financial/monetary.
Most financial systems simply store prices/dollar values in cents, you can write a function to format it nicely for users if there is a need to display the values.
function(cents) {
cents = +cents; // unary plus converts cents into a string
return "$" + cents.substring(0, cents.length - 2) + "." + cents.substring(cents.length - 2);
}
I was wondering if anyone knew how to find the base of an exponential equation in Javascript.
I couldn't find any function that allows this to be done (e.g. using the Math functions).
For example, how do I find 'b' in the following equation:
y = b^t
Thanks in advance for any help you can provide.
If you know what are the values of y and t are, you can get the value of b by calculating the t-th root of y like this:
Math.pow(y, 1/t);
Source:
JavaScript: Calculate the nth root of a number
What you need is math and the logarithm.
y = b^t
=> t = log(y) / log(b)
=> log(b) = log(y) / t
=> b = 10 ^ ( log(y) / t )
So it would be something like
b = Math.pow(10, (Math.log(y) / t));
-Hannes
I'm trying to resolve a small programming challenge, which is calculating the nth number of the Golomb's sequence (see this for more help). I've written a simple solution, but it may have any problem, because the number at 2500000 position is 10813 but my program gives me 10814.
var golomb = (function(){
var cache = [null, 1];
const o = 0.5 * (1 + Math.sqrt(5)); // Golden ratio
return function(n){
return cache[n] || (function(){
return Math.round(Math.pow(o, 2-o) * Math.pow(n, o-1));
})();
};
})();
var num = golomb(process.argv[2]);
console.log(num);
Maybe, the golden ratio needs more lenght than JavaScript gives. Someone can help? Thanks.
For what it's worth, here is a function based on the recurrence relation, with a cache, that gives the correct answer pretty quickly
var golomb = (function() {
var cache = [null, 1];
return function(n) {
var i;
for (i=cache.length;i<n;i++) cache[i]=golomb(i);
return cache[n] || (cache[n]=1+golomb(n-golomb(golomb(n-1))));
}
})();
check it up on jsFiddle
Sgmonda, the formula you got from Wolfram Alpha isn't an exact solution. I've actually complained to them, since I like the Golomb sequence. The recurrence relation is exact but slow, even if you cache it. Heh, that's why the programming challenge is a challenge.
From the wikipedia article:
Colin Mallows has given an explicit recurrence relation:
a(1) = 1;
a(n + 1) = 1 + a(n + 1 − a(a(n)))
You need to implement your solution in this iterative method that uses integers.
A quick attempt at trying to implement that gives:
function golomb(n) {
if(n == 1) return 1;
else return 1 + golomb(n − golomb(golomb(n-1)));
}
Hi I need help creating a javascript calculator. Unfortunately, I'm not even sure how to search for an answer so I thought I would start here.
I need a calculator that multiplies a number based on an input. For example
Gross income
Number of Children 1, 2, 3, etc
If 1 child multiply gross income by 20%
if 2 multiply gross income by 25%
if 3, etc.
And then obviously it spits out a value.
I would really appreciate some guidance on where to go to try something like this out. Thanks in advance.
Try this:
var income = 100;
var children = 3;
var multiply = 0.15 + (children * 0.05);
var result = income*multiply;
As you required, I'll give you only some guidances to get this done. Indeed, it's pretty basic and you won't need us to give you the whole code.
Get the gross income value: http://www.javascript-coder.com/javascript-form/javascript-get-form.phtml
For the ratio, it depends on how you made the field:
Selectbox with key => value (for instance 1 => 0.20, 2 => 0.25): ratio = value
Textfield: ratio = 0.15 + value * 0.05
Then, calculation: result = ratio * grossIncome
var income = 100;
var children = 3;
var multiply = eval(0.15 + (children * 0.05));
var result = income*multiply;
use eval() for this,
If the argument is an expression, eval() evaluates the expression. If the argument is one or more JavaScript statements, eval() executes the statements.