0

I am new to programming, and I have been working on a javascript problem for the past couple of days.

Essentially, I need to write a function that takes in three parameters: an array arr, a function fn to be called repeatedly, and a value step that signifies how many elements from the array to pass into fn.

For example, if step were 3, then fn would be called with the first 3 elements of arr each as a positional argument. fn will be called again with the next 3 arguments. fn will continue to be called until there are no more elements to use as arguments from arr. If the last group of arguments is less than step, call the function fn with whatever arguments are left.

I am pretty new to recursion. I have watched lots of videos and was attempting practice problems from my textbook and this was one of the ones I had trouble with.

Any help would be appreciated. Thanks!

fubar
  • 16,918
  • 4
  • 37
  • 43
  • Does the function return anything? – yccteam Sep 24 '18 at 22:02
  • This problem doesn't involve recursion. If this is a learning exercise, post what you've tried, and we can advise from there. – fubar Sep 24 '18 at 22:03
  • I agree with @fubar. Seems like a simple loop would solve this: function arrayHandler(arr, cb, nArgs) { for (let i = 0; i < arr.length; i += nArgs) { cb(arr[i], arr[i+1], arr[i+2]); } } – yccteam Sep 24 '18 at 22:09
  • @yccteam, you may want to use `Array.prototype.slice`, because your solution doesn't support a change in the step values. – fubar Sep 24 '18 at 22:11
  • the function fn being called is unknown. we dont know what the function does or returns. @fubar, also the problem is supposed to be solved with recursion. I tried using a while arr not empty loop and inside a for loop that iterates to step elements, puts them in a diff array, then passes the values of that to fn but I am not supposed to use any loops and the problem is supposed to be solved only with recursion ;( – Pranay Patel Sep 24 '18 at 22:16
  • @fubar - I didn't understand that from the specifications. arrayHandler should be called once only. Did I get this wrong? – yccteam Sep 24 '18 at 22:16
  • @yccteam, my understanding was that the `step` value could change. But your call to `cb(arr[i], arr[i+1], arr[i+2])` passes through a fixed number of parameters. – fubar Sep 24 '18 at 22:21
  • these are the examples i got: steppedForEach([1, 2, 3, 4, 5, 6], (a, b, c) => console.log('' + a + b + c), 3); /* note that arrow function is called twice, each time with 3 elements from arr: 123 456 */ steppedForEach([1, 2, 3, 4, 5, 6, 7], (a, b, c) => console.log('' + a + b + c), 3); /* in this case, the arrow function is called 3 times, but the last time, only one argument is passed in, 7 (there are no more elements after that) 123 456 7undefinedundefined */ Note stepForEach is the name of the method I am implementing – Pranay Patel Sep 24 '18 at 22:23
  • @PranayPatel, I see. I didn't understand that you **had** to use recusion. I thought this was your suggestion. Have you managed to attempt this yourself? – fubar Sep 24 '18 at 22:23
  • @fubar ofc! I honestly don't even know how to tackle this without loops. my guess, even with recurssion, would be to have a while loop with a for loop that iterates through the array and recurses through every "step" elements and that is what I tried but came to find out I can't include any loops. :/ – Pranay Patel Sep 24 '18 at 22:24
  • @fubar - you are correct. Happens when you are trying to think at 1 A.M. :) – yccteam Sep 24 '18 at 22:26

2 Answers2

0

if you have to use recursive function you can try

function withRecursion(array, fn, value){
  //take an amount of arguments equal to value by slicing the array
  fn( ...array.slice(0, value) );
  //if the length of array is lower then value this means that this was the last elements of the array
  if(value < array.length){
    //give the function the rest of the array by removing the elements we already used
    withRecursion(array.slice(value, array.length), fn, value); 
  }
}

function test(...args){
  for(var arg of args){
    console.log(arg);
  }
  console.log("test finished");
}

withRecursion([1,2,3,4,5,6,7,8,9,10], test, 3);
Stakvino
  • 642
  • 4
  • 9
0

So basically you want a function that processes data in steps:

var arrayToProcess = [1, 2, 3, 4, 5, 6, 7, 8]

function myRecursiveFunction(step, fn, arr) {
  // if the step is greater that the array, process and return
  if (arr.length <= step) {
    fn(arr);
    return;
  }
  // else take out the part of the array that you will process on this step
  myRecursiveFunction(step, fn, arr.slice(step));
  // process the array with only the element belonging to the step
  fn(arr.slice(0, step));
  return;

}

myRecursiveFunction(3, (function(arr) {
  console.log(arr);
}), arrayToProcess);

So, what's going to happen is that we're going to be calling the recursive function with these inputs in this order:

  1. myRecursiveFunction(3, fn, [1,2,3,4,5,6,7,8])

  2. myRecursiveFunction(3, fn, [4,5,6,7,8])

  3. myRecursiveFunction(3, fn, [7,8])

Which in turn will execute the input fn in this order:

  1. fn([7,8])

  2. fn([4,5,6])

  3. fn([1,2,3])

Our stop condition is if the arr is smaller then the step: arr.length <= step else, we will continue recursion and also process the first step elements of the arr.

If you want to understand how the slice function works you can look at it here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/slice

fnmps
  • 143
  • 6