I’m working on this Math recursive question.
What is needed to do is when I put in 0 to the console, it should display the answer then take that answer put into the equation and display the next answer, it should continuing doing this till the value indicate on the loop.
At the moment this is my code:
const pw= (x) =>{
sum = []
for(let i=0; i<5; i++){
let a = (x*x*x)
sum+= ((4-a)/10)
}
return sum
}
So if I put in 0 the answer is 0.4, then it should take that answer (0.4) automatically put it through the code and return the next answer which is 0.3 and so on.
At the moment that’s where i’m stuck i dont know how to automatically put the next vaule in.
Any guidance will be appreachiated.
console.log(pw(0))
(0.4)
(0.3)
(0.3973)
(0.3937287271683)
2
Answers
You can use a recursive function like this. This will get you your required answer.
OR
If you want to use for loop.
If you really must do it recursively, then you can do something like
Though, that’s not very good, since if you pass in a second argument it goes boom – so, another recursive way
However, no need for recursion at all