I have a function that I need to handle in Javascript:
equipment * ((rate/12) / ((1-(1+(rate/12))^-term)))
How do I convert it (^) into Javascript?
This is what it looks like so far:
const calculation = equipment * ((rate / 12) / ((1 - (1 + (rate / 12)) - term)));
JavaScript has an operator for it:
console.log(3 ** 5);
Try using this:
let result = Math.pow(3, 5)
//or
let result = 3 ** 5
This is equivalent to 3^5
Related
Recently i started to learn recursion in JavaScript.
i never called defined function in its own function.
what is this called?
i'm unable to understand how this recur(n - 1) working.
i tried to check how it working in chrome debugger, but there is no output too.
How here recur(n - 1) working?
function recur(n) {
if(n === 1) return 1
return n * recur(n - 1);
// How here recur(n - 1) working?
// 5 * 5 - 1 = 24
// 4 * 4 - 1 = 15
}
recur(5);
So when you do return n * recur(n - 1), the first thing that is calculated is recur(n - 1).
What happens is the following
recur(5) = 5 * recur(5 - 1)
recur(4) = 4 * recur(4 - 1)
recur(3) = 3 * recur(3 - 1)
recur(2) = 2 * recur(2 - 1)
recur(1) = 1 => the bottom of the recursion. Now it starts to go back:
recur(2) = 2 * recur(1) = 2
recur(3) = 3 * recur(2) = 3 * 2 = 6
recur(4) = 4 * recur(3) = 4 * 6 = 24
recur(5) = 5 * recur(4) = 5 * 24 = 120
Basically, until a function of recur(n - 1) returns something concrete, like the bottom of the recursion in your case 1, the method calls recur once again, and again and so on. After you go to the bottom, then the functions start to resolve in the reverse order.
The recursive function is calculating the factorial of n.
The function will call itself until it reaches a base case.
You have one base case: when n is 1 then return 1.
If you call it with say 5 then it works as follows:
recur(5) =>
5 * recur(4) =>
5 * 4 * recur(3) =>
5 * 4 * 3 * recur(2) =>
5 * 4 * 3 * 2 * recur(1) =>
5 * 4 * 3 * 2 * 1 =>
120
So the result will be 120 for n=5.
How to calculate fibonacci function by math formula
I have try this formula but not work:
fib(n) = ((1 + 5^0.5) / 2)^n - ((1 - 5^0.5) / 2)^n / 5^0.5
const fib=(n)=>{
return ((1+(5**0.5))/2)**n-((1-(5**0.5))/2)**n/(5**0.5)
}
Any one know how to do?Thanks.
The formula is correct you just need to put some extra ().
const fib=(n)=>{
return (((1+(5**0.5))/2)**n-(((1-(5**0.5))/2)**n))/(5**0.5)
}
for(let i = 0;i<9;i++){
console.log(fib(i))
}
First thing I'd do is define φ
var φ = (1 + 5 ** 0.5) / 2;
Then a slightly shorter form is:
var fib = (n) => (φ ** n - ((-φ) ** -n)) / (2 * φ - 1);
Because you want an integer as a result, you could also throw in the call to Math.round().
You seem to be trying to recreate Binet's Formula. You can break down the formula a little like so, such that is becomes more readable:
const fib = n => {
const alpha = 5**0.5
const beta = alpha / 2;
return (1 / alpha) * ((0.5 + beta) ** n - (0.5 - beta) ** n);
}
console.log(fib(10));
I am trying to port a formula from numbers (Mac OS 10) to javascript, the original formula is thus:
(1−(1+(C37÷100)÷C39)^−C45)÷((C37÷100)÷C39)
I have written this:
var calcValueOne = (1 - (1 + (yield/100) / coupon_frequency) Math.sqrt() - coupons_until_maturity) / ( (yield/100) / coupon_frequency);
where yield = C37 and coupon_frequency = C39 and coupons_until_maturity = C45
I am getting the following error:
SyntaxError: Unexpected identifier 'Math'. Expected ')' to end a
compound expression.
My mathematics is not to hot and transposing this in to Javascript is proving to be a huge challenge for me, can anyone post a solution to this please?
EDIT
#Musa kindly added a response but the number it is returning is not what I expect so I thought I would expand on the values to see if that helps.
yield (C37) == 9.89
coupon_frequency (C39) == 4.00
coupons_until_maturity (C45) == 15.00
The number I am expecting is 12.41 but I am getting -607.1703544847592 the javascript now looks like this:
var calcValueOne = (1 - Math.sqrt(1 + (yield/100) / coupon_frequency) - coupons_until_maturity) / ( (yield/100) / coupon_frequency);
Again for clarity this is the original (excel / numbers) formula:
The ^ sign is the exponent function not the square root so use Math.pow to calculate it
var yield = 9.89;
var coupon_frequency= 4.00;
var coupons_until_maturity = 15.00
var calcValueOne = (1 - Math.pow((1 + (yield/100) / coupon_frequency), - coupons_until_maturity)) / ( (yield/100) / coupon_frequency);
document.body.innerHTML = calcValueOne;
var x = 1 + Math.Random() % 9;
if (x==1)
// do something
else if (x==2)
// do something else
I used this line — (1 + Math.Random() % 9) — in C++ to get a number between 1 and 9, but in JavaScript I'm getting a different result.
Math.random() returns a value between 0 and 1, so instead using the modulo operator you need to use a multiplication.
1 + (Math.random() * 9);
Finally, you should round or .floor() that value
var x = Math.floor( 1 + ( Math.random() * 9 ) );
or, shorter
var x = ~~( 1 + ( Math.random() * 9 ) );
There is no Math.Random() function in JavaScript. It's Math.random(). Note the capitalization.
To get a random number between a certain minimum and maximum value, do this:
var min = 1, max = 9;
Math.floor(Math.random() * (max - min + 1)) + min;
Further reading: https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Math/random
In Javascript the Math.random function returns a number between 0 and 1. If you want to get a number between 1 and 9 you'll have to work with it a bit.
var number = ((Math.random() * 10) | 0) % 9 + 1
This will give you a result between 0 and 9
Math.floor(Math.random()*9)
And by the way, jQuery is a javascript framework. Math is a native javascript function
I need to perform a math calculation in JavaScript that I am unsure how to do so.
The formula is as follows:
result = (scale * weighting) / 2
where the result I will need to two decimal places.
Example formula that I need to do in JavaScript might be:
result = ((3 * 2) + (2 * 1.5)) / 2
I am also unsure if I have to also convert this to int.
toFixed:
function algorithm(scale, weighting) {
var result = (scale * weighting) / 2;
return result.toFixed(2);
}
Or if you need an integer, parseInt:
parseInt(result, 10);
Not entirely sure what you want, but this might help:
result = Math.round(((scale * weighting) / 2) * 100) / 100;
http://jsfiddle.net/kQpma/
In JavaScript everything number is a floating point number. No explicit type conversion needed.
ref : http://oreilly.com/javascript/excerpts/learning-javascript/javascript-datatypes-variables.html ( search for "The Number Data Type" )