How do I use the '^' in this function in JavaScript? [duplicate] - javascript

This question already has answers here:
What does the ^ (caret) symbol do in JavaScript?
(5 answers)
Closed 5 years ago.
Write a function called "computeCompoundInterest".
Given a principal, an interest rate, a compounding frequency, and a
time (in years), "computeCompoundInterest" returns the amount of
compound interest generated.
var output = computeCompoundInterest(1500, .043, 4, 6);
console.log(output); // --> 438.8368221341061
Reference:
https://en.wikipedia.org/wiki/Compound_interest#Calculation_of_compound_interest
This shows the formula used to calculate the total compound interest generated.
Problem is i am trying to use this formula and can't get it right
function computeCompoundInterest(p, i, compoundingFrequency, timeInYears) {
p = p * (1 + (i/4)))^(compoundingFrequency*timeInYears)
}
I tried to step through each calculation and it looks like once I get to:
p = 1500 * (1 + 0.043/4)^ 4 x 6 //compoundingFrequency = 4 and timeInYears = 6
I am doing something wrong. This website seems to get a decimal number when you (1 + (i/4)))^(compoundingFrequency*timeInYears)

^ operator is XOR operator.
For exponentation you should use function Math.pow (like Math.pow(2,3) === 8) or ** operator (like 2**3 === 8)

Related

I'd like to ask the user to input a number again when entering a string using the while, but it comes out as NaN and the while.. does not work [duplicate]

This question already has answers here:
How do you check that a number is NaN in JavaScript?
(33 answers)
Closed last year.
let getInch = prompt('Cm로 바꾸고 싶은 Inch 값을 넣으세요','숫자만 입력하세요');
let numInch = Number(getInch);
while (numInch == NaN) {
alert(`숫자만을 입력하세요`);
getInch = prompt('Cm로 바꾸고 싶은 Inch 값을 넣으세요');}
let numCm = Math.round(numInch * 25.4) / 10;
alert(`${numInch}인치는 대략 ${numCm}센치미터 입니다`);
Use isNAN() method instead of == NaN
https://developer.mozilla.org/de/docs/Web/JavaScript/Reference/Global_Objects/isNaN

nth roots in JavaScript [duplicate]

This question already has answers here:
JavaScript: Calculate the nth root of a number
(9 answers)
Closed 1 year ago.
I was working on a simple little math project that is intended to help with exponential functions and stuff. I want to calculate a from f(x), x and c. So the Formula for that is pretty simple, it's just the x root of f(x) divided by c. But I can't find a way to take a specific nth root. I read something about Math.pow() and played around with it, but can't really get it working.
Those are my current lines of code:
if (fx != null && c != null && x != null) {
var a = <the part i need>
aoutp.innerHTML = "a = " + a;
}
Hope you understand what I need :)
Math.pow(x, 1/y) is y-root of x:
const pow_4 = Math.pow(2, 4) // 2^4
console.log(pow_4) // 16
const sqrt_4 = Math.pow(pow_4, 1/4) // sqrt root lvl 4 of 16
console.log(sqrt_4) // 2

Why is my function returning "newLine.unshift(startN) is not a function" [duplicate]

This question already has answers here:
Parameters as storage in recursion - Return an array using recursion of all values from startNum to endNum
(4 answers)
Closed 1 year ago.
I have been trying to write a recursive function to get a range of numbers, but I am getting an error saying 'newLine. unshift(startN) is not a function'.
Following is the code:
function rangeOfNumbers(startN, endN) {
if (startN - endN === 0) {
return "The starting number will always be less than or equal to the ending number";
} else {
const newLine = rangeOfNumbers(startN + 1, endN);
newLine.unshift(startN);
return newLine;
}
}
console.log(rangeOfNumbers(1, 7));
Can someone please help me to find the reason for getting the error mentioned?
rangeOfNumbers(1, 7) / newLine = rangeOfNumbers(2, 7)
rangeOfNumbers(2, 7) / newLine = rangeOfNumbers(3, 7)
...
rangeOfNumbers(7, 7) / newLine = "The starting number will always be ..."
typeof newLine = String
newLine.unshift() >> undefined // unshift isn't available JS Strings

Round the number with JavaScript [duplicate]

This question already has answers here:
How do I round a number in JavaScript?
(8 answers)
Round number up to the nearest multiple of 3
(13 answers)
Closed 4 years ago.
I want to round the numbers like this:
Value Expected
0,523% 1%
2,235% 2,5%
-0,081% -0,5%
-1,081% -1,5%
How can I do this with JavaScript?
Solution:
static round (num) {
const abs = Math.abs(num);
const sign = num < 0 ? -1 : 1;
return sign * (abs % 1 > 0.5 ? Math.ceil(abs) : Math.floor(abs) + 0.5) }
}
I used Excel for it =round(Value/Granularity)*Granularity but I tried it in JavaScript not working.
static calc(num){
const granularitiy = 0.00005;
let calc = Math.round((num / granularitiy)) * granularitiy;
return calc;
}
My granularity value is 0.00005;
Since on only want to round to .5 or round numbers, you can't simply use Math.round(number).
The easiest way to do this would be to have something like:
Math.round(number*2)/2
This way, you'll alway have a number .5 or round.
If you want to round another way (ceil or floor), you can simply replace the Math.round by whatever you want.

how can round the number in specific format? [duplicate]

This question already has answers here:
Javascript roundoff number to nearest 0.5
(10 answers)
Closed 6 years ago.
I want to round the number in specific format in jquery
For ex.
var First = 3.52
if last value after decimal point 2 is less then 3 then it should be round to 0
if last value after decimal point 2 is beetween then 3 to 7 then it should be round to 5
if last value after decimal point 2 is more then 7 then it should be round to 10
Like
if(var First = 1.21) { var Answer = 1.20 }
if(var First = 1.24) { var Answer = 1.25 }
if(var First = 1.28) { var Answer = 1.30 }
i have tried Round and Ceil , Floor but doesnt work for me
To achieve this you can use Math.round() to the nearest 0.05, like this:
Math.round(number * 20) / 20;
To make it easier you can extract this to a function:
console.log(round(1.21)); // = 1.20
console.log(round(1.24)); // = 1.25
console.log(round(1.28)); // = 1.30
function round(num) {
return (Math.round(num * 20) / 20).toFixed(2);
}
Note the use of toFixed(2) to force the result to 2 decimal places. Beware that this method returns a string, so if you are planning on doing any calculations with the value you will need to run the result through parseFloat().

Categories

Resources