0

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 :)

Titus
  • 39
  • 6
  • 2
    I recommend not using names like `a` and `c` and `fx` for variables. You should use descriptive names that better depict what they are doing. – Jeff B Jun 01 '21 at 15:16
  • 1
    https://stackoverflow.com/questions/7308627/javascript-calculate-the-nth-root-of-a-number – epascarello Jun 01 '21 at 15:17
  • I get what u mean, but that are the correct mathematical names for these. So everybody who understands maths and can help me will probably get what i am talking about. @Jeff B – Titus Jun 01 '21 at 15:18
  • 1
    @Zuckerpapa I understand, there is a general push that when you convert math to code that you also adhere to proper naming conventions as well. It will server you well. – Jeff B Jun 01 '21 at 15:21
  • _“I read something about Math.pow() and played around with it, but cant really get it working.”_ — But that’s exactly what you need. Why not show exactly what you’ve tried? – Sebastian Simon Jun 01 '21 at 15:27

1 Answers1

1

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
ulou
  • 5,542
  • 5
  • 37
  • 47
  • So if i get this right, i need to do: var a = Math.pow(fx/c, 1/x) to get the x-root of fx divided by c right? Because i did this and it just says "Infinity" no matter what the input is. – Titus Jun 01 '21 at 15:26
  • @Zuckerpapa `Math.pow(fx/c, 1/x)` is _ˣ_ √( _fx_ ∕ _c_). Is that what you need? Please try using the [debugging capabilities](//developer.mozilla.org/en-US/docs/Mozilla/Debugging/Debugging_JavaScript) of your browser. – Sebastian Simon Jun 01 '21 at 15:29
  • @Sebastian Simon Yes it is. I gave it the inputs fx = 1000, c = 500 and x = 5. It should output 1,15 (if rounded) but as i said, it outputs Infinity instead. – Titus Jun 01 '21 at 15:32
  • @Zuckerpapa `Math.pow(1000 / 500, 1 / 5)` is `1.148698354997035`. Again, please try using the [debugging capabilities](//developer.mozilla.org/en-US/docs/Mozilla/Debugging/Debugging_JavaScript) of your browser to confirm that `fx`, `x`, and `c` are the values you expect. – Sebastian Simon Jun 01 '21 at 15:34
  • @Zuckerpapa It's not `NaN` your code must be incorrect somewhere. Don't you have something like this: `Math.pow(1000/500/1/5)` or `Math.pow(1000/500.1/5)`? – ulou Jun 01 '21 at 15:34
  • @ulou i used console.log() to output the given numbers in the console and for some reasons everything is fine except of x being instead. Guess i have to go bug hunting -_- Anyways thx for the support – Titus Jun 01 '21 at 15:37