'How can I change and keep the value of a variable inside a recursive function without passing it as a parameter?
function newtonRaphson(x, count){
while(count > 0){
var previousValue = 0
if(previousValue === 0){
previousValue = x/2
}
var y = (previousValue + (x/previousValue))/2
previousValue = y
console.log(previousValue, count)
return newtonRaphson(x, count - 1)
}
return y
}
I have this function that is supposed to perform the Newton Raphson method, it would look like this:
- x = 20
- count = 4
- previousValue(0) = 10
- y(1) = (10 + (20/10))/2 = 6
- previousValue = 6
- y(2) = (6 + (20/6))/2 = 4.666
- previousValue = 4.666
- y(3) = (4.666 + (20/4.666))/2 = 4.476
- previousValue = 4.476
- y(4) = (4.476+ (20/4.476))/2 = 4.472
My question is, is there a way I can maintain previousValue between iterations without passing it to the function? Because x needs to be passed no matter what, as well as count, but I also need to tell the function what the previousValue is, so I'm at a loss
Solution 1:[1]
This code is confused.
First off, while it's called newtonRaphson, it seems to be only for a very specific case of the Newton-Raphson method, that of finding square roots using the Babylonian Method. So ideally it should have a better name.
It tries to simultaneously solve the problem recursively and with a while-loop. We need to choose one or the other. Here we choose a recursive version.
Then we note that these lines make no real sense:
var previousValue = 0
if(previousValue === 0){
previousValue = x/2
}
This is a ridiculously round-about way of writing
var previousValue = x/2
You ask about doing this without passing the previous value to the recursive function. You could, just by tracking it at a higher scope. (Please, please, not the global scope!) It might look like this:
const sqrt = (x, count) => {
let prev = x / 2
const _sqrt = (count) => {
if (count <= 0) return prev
prev = (prev + (x / prev)) / 2
return _sqrt (count - 1)
}
return _sqrt (count)
}
console .log (sqrt (25, 1)) //=> 7.25
console .log (sqrt (25, 2)) //=> 5.349137931034482
console .log (sqrt (25, 3)) //=> 5.011394106532552
console .log (sqrt (25, 4)) //=> 5.000012953048684
console .log (sqrt (25, 5)) //=> 5.000000000016778
console .log (sqrt (25, 6)) //=> 5
But I would not recommend this at all. Recursion is simplest when you pass the necessary variables into the function. And this is not hard to do, either with a defaulted parameter, like this:
const sqrt = (x, count, prev = x / 2) =>
count <= 0
? prev
: sqrt (x, count - 1, (prev + (x / prev)) / 2)
or with a public wrapper around an internal recursive helper function, like this:
const _sqrt = (x, count, prev) =>
count <= 0
? prev
: _sqrt (x, count - 1, (prev + (x / prev)) / 2)
const sqrt = (x, count) =>
_sqrt (x, count, x / 2)
Of the two, I usually prefer the defaulted parameter, but there are some potential problems with them, especially if you are not in control of how your function is called. Then the public/internal split makes sense.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Scott Sauyet |
