Given a callback function we have to iterate over a callback n times. The callback is a function that is passed as an argument. for iterate over callback function, we have to run callback function n time.
Approach 1: We use recursion to iterate n times callback function.
- First create callback function factor which takes n as argument.
- Factor function generate pattern of n length.
- Create test function that takes callback function and n.
- Test function checks value of n is equal to 0 and not.
- If n is 0 it return terminate test function, else it call callback function which print the pattern.
01 02 03 04 01 02 03 01 02 01
Approach 2: We use loop statement to iterate over callback.
- First we create callback function factor which generate factorial of number.
- Create test function with argument n and callback function.
- Check value of n if it in invalid terminate if not continue.
- Create for loop with range n.
- On each loop call callback function which print factorial of each number.
factorial of 5 is 120 factorial of 4 is 24 factorial of 3 is 6 factorial of 2 is 2 factorial of 1 is 1