4

hello I'm new learning recursive but i don't know to solve with this problem, cause this one using 2 parameters, I don't know how to return with 2 arguments.

this is the normal loop ,not a recursive yet cause i dunno how to change it to recursive:

function deret2(num,num2){
  var tampung = 1;
   for (var i= num; i >= 1 ; i-= num2){

    tampung = tampung * i ;
  }
  console.log(tampung);
}
deret2(12,5); //12* 7 * 2 = 168

Edit : the recursive I made :

function deret(num,num2) {
  //var tampung = 1;
  if (num <= 0) { // terminal case
    return 1;
  } else 
  { // block to execute
    return num * deret(num-num2,num2); 
  }
};
deret(12,5);

it won't work cause number* deret(2 arguments here) ?? that's why i don't know how to make this a recursive with 2 parameters, how could u save it and multiply it against 2 arguments ?

10
  • 1
    Return where? There's neither return statement nor this is a recursive function. Commented Nov 16, 2016 at 15:35
  • yeah i want to change it to recursive , this is normal loop Commented Nov 16, 2016 at 15:36
  • 1
    So you will need a function that returns num * deret2(num-num2,num2). And don't forget your base case... Commented Nov 16, 2016 at 15:37
  • 1
    @Mike: A for loop and recursion are not the same thing. Commented Nov 16, 2016 at 15:41
  • 1
    So what's wrong with your recursive function? It doesn't need any of the tampung stuff, but it should work. Commented Nov 16, 2016 at 15:45

3 Answers 3

8

You could use a recursive function with check.

You need for a recursive function a check if the recursion should stop, or if the function should call again with same or changed paramters. Then you need a value to return in both cases.

Rule of thumb, for multiplications return as last value 1 and for addition 0.

num  num2  return
---  ----  ------------------
 12     5  12 * deret2(7, 5)
  7     5   7 * deret2(2, 5)
  2     5   2 * deret2(-3, 5)
 -3     5   1

function deret2(num, num2) {
    return num >= 1 ? num * deret2(num - num2, num2) : 1;
}

console.log(deret2(12, 5)); //12* 7 * 2 = 168

With if syntax.

function deret2(num, num2) {
    if (num >= 1) {
        return num * deret2(num - num2, num2);
    } else {
        return 1;
    }
}

console.log(deret2(12, 5)); //12* 7 * 2 = 168

Bonus: The shortest version.

function deret2(num, num2) {
    return +(num < 1) || num * deret2(num - num2, num2);
}

console.log(deret2(12, 5)); //12* 7 * 2 = 168

Sign up to request clarification or add additional context in comments.

14 Comments

You you you.. Always come with the best solution! :P
Not sure if someone who asks what a recursion is could understand a short-circuit? Anyway nice and short answer!
damn so it works with 12* funct(7,2) ... how does it multiply ? why 12 not multiply to the 2nd number too ( ,2)?
the second parameter stays with 5.
I agree with @Megajin that while very concise, it's not exactly the most readable form for the OP. Perhaps edit in the longer form to better illustrate what's happening?
|
3

A recursive function calls itself, and with an extra parameter, we don't need to keep track of a mutating variable. Looping and recursion are very closely related, to the point where the transformation is mechanical.

function deret2(num,num2,tampung){
  if (num >= 1) {
    deret2(num - num2, num2, tampung * num);
  } else {
    console.log(tampung);
  }
}
deret2(12,5,1); //12* 7 * 2 = 168

Note that we're passing in the initial value of tampung directly now, instead of encoding it into the recursion. If you don't want the caller to pass the base value of tampung, it's fairly common to make a helper function to encode that base case and then start the recursion.

2 Comments

There is no need for an extra parameter at all
There is if you want tail recursion... but you're right, could just keep descending and multiplying num * deret2(...)
3

A recursive function should have itself declared inside the function AND a stop if condition:

function deret2(startNumber,step){
   if(startNumber>= 1){
     return startNumber * deret2(startNumber - step, step);
   }else{
     return 1;
   }
}
deret2(12,5); //12* 7 * 2 = 168

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.