I can not get out of recursion

function consoleRec(sourse) { console.log(sourse.shift()); consoleRec(sourse); } consoleRec(['я', 'умею', 'писать', 'рекурсивные', 'функции']); 

Condition: It is forbidden to use loops and methods for working with arrays. The function must take two arguments: an array and ... something else. What exactly is left to your discretion.

Call example:

 consoleRec(['я', 'умею', 'писать', 'рекурсивные', 'функции'], ???); 

Should display:

 я умею писать рекурсивные функции 
  • it is forbidden to use arrays but the function can take arrays is logical - Excess Gophers
  • 2
    @ Excess gusters, it is forbidden to use methods for working with arrays , not arrays themselves - Grundy

4 answers 4

The implementation of the recursive algorithm should always contain:

  • Recurrence ratio
    This is how we reduce the solution of the problem to the solution of smaller problems.
  • Extreme cases
    This is what will help us to stop in time, some trivial cases that can be solved on the spot, without resorting to splitting the task into subtasks.

In your implementation, you forgot about the second.

 function consoleRec(src) { if (src.length === 0) return; // крайний случай, пустой массив console.log(src.shift()); // действие текущей итерации consoleRec(src); // рекурсивный вызов для уменьшенного массива } consoleRec(['я', 'умею', 'писать', 'рекурсивные', 'функции']); 


Referring to the condition of your task

It is forbidden to use loops and methods for working with arrays

we cannot change the original array (or cut off parts of it), so the second parameter will have to pass the current size of the task:

 /* Передача размера задачи */ function r1(src, taskSize) { if (taskSize === 0) return; r1(src, taskSize-1); console.log(src[taskSize-1]); } /* Передача индекса начала необработанной части массива */ function r2(src, start) { if (start === src.length) return; console.log(src[start]); r2(src, start + 1); } let list = ['я', 'умею', 'писать', 'рекурсивные', 'функции']; r1(list, list.length); r2(list, 0); 

    I can not get out of recursion

    This is how you can implement it:

     function consoleRec(source, i) { // Условие для выхода if (source.length - 1 >= i) { // Печать console.log(source[i]); // Вызов рекурсии consoleRec(source, i + 1) } } consoleRec(['я', 'умею', 'писать', 'рекурсивные', 'функции'], 0); 

       let res = ''; function consoleRec(arr, indx = 0) { res += arr[indx] + '\r\n'; indx++; if (indx < arr.length) { consoleRec(arr, indx); } return res; } //Вызов const arr = ['я', 'умею', 'писать', 'рекурсивные', 'функции']; console.log(consoleRec(arr, 0)); 

         void function r(f, arr, i = 0) { f(arr[i]) || i++ < arr.length - 1 && r(f, arr, i) }(console.log, ['я', 'умею', 'писать', 'рекурсивные', 'функции']) 

        by the way, the problem statement does not say about recursion

         let f = (a, b) => b(('' + a).replace(/,/g, '\n')) f(['я', 'умею', 'писать', 'рекурсивные', 'функции'], console.log)