Home > Mobile >  Compound interest with recursion: Hey guys, idk why it says my return function is error?
Compound interest with recursion: Hey guys, idk why it says my return function is error?

Time:02-25

idk why it says my return function recursion(M, rate, day-1) * (100.0 rate)/100
is error?, and btw M is ammount and StartM is startammount.

var M; 
var StartM = 10000; 
var rate = 0.01;
function setup() {
createCanvas(400, 400);
function recursion(M, rate, day){
if(day == 0)
return 1;  
else

return function recursion(M, rate, day-1) * (100.0   rate)/100

print(day   "   "  M);

}

function draw() {
background(220);

} }

CodePudding user response:

Remove function word from return. Also, you have to call your recursion function.

var M; 
var StartM = 10000; 
var rate = 0.01;
function setup() {
createCanvas(400, 400);
  draw()
  recursion(StartM,rate,10)
function recursion(M, rate, day){
  print(day)
if(day == 0)
return 1;  
else

return  recursion(M, rate, day-1) * (100.0   rate)/100

print(day   "   "  M);

}

function draw() {
background(220);
} 
}

CodePudding user response:

so in this code below, i can see how much i gained in 20 days, in the console, but its made with a for loop. How can I change that into a recursive function
instead of a foor loop?

var M;
var startM = 10000;
var rate = 0.01;
function setup() {
  createCanvas(400, 400);
  
  for(var day =1; day <=20;day   ){
    M=startM*pow(1   rate, day);
    print(day   "   "  M);
  }
}

function draw() {
  background(220);
}
  • Related