Click here to Skip to main content
15,745,270 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Test Cases:

addToTwelve([1, 3, 4, 7, 5]); // true
addToTwelve([1, 3, 4, 7, 6]); // false
addToTwelve([1, 11, 4, 7, 6]); // true
addToTwelve([1, 12, 4, 7, 6]); // false
addToTwelve([1]); // false

I feel like this is more of a callback than recursion since I am iteratively going through the array and then recalling the function. But, I'm recalling the function on itself until a basecase is met,yet I'm using a for loop which is iterative..

I think, recursively, what I need to do is return the array and shift first element and then recheck if the first + second = 12 and if true return true. Base case being array.length < 1.

What I have tried:

function addToTwelve(array) {
  if (array.length < 1) {
    return false;
  } else {
    for (let i = 0; i < array.length; i++) {
      if (array[i] + array[i+1] === 12) {
        return true;
      } else {
        return addToTwelve(array);

I refactored it without the for loop but recursion is really busting my brain here.

function addToTwelve(array) {
  if (array.length < 1) {
    return false;
  } else if (array[0]+array[1] === 12) {
    return true;
  } else {
    return addToTwelve(array)

Is that considered recursion now? XD Can you show me perhaps a better way to complete this?
Updated 10-Oct-22 19:33pm

1 solution

Technically, it's recursion - because the function does call itself.
But ... it's a poor example in both cases.

The first example runs off the end of the array:
for (let i = 0; i < array.length; i++) {
  if (array[i] + array[i+1] === 12) {
If you have three elements, teh valid indices are 0, 1, and 2 - but your loop would add:
array[0] + array[1]
array[1] + array[2]
array[2] + array[3]
If it ever got to a second iteration. Which it won't because there is a return on both sides of the if.
So the loop is redundant: it does nothing.

The second version is technically recursion, but in practice it's just a loop that wastes stack space and which will crash with big arrays as a result!

Why would you want to make this a recursive function? It's not a recursive problem, it's iterative!
Share this answer
Chris Aug2022 11-Oct-22 1:41am    
Thanks Griff, it's part of App Academy Open Course, 1 of 18 recursive problems. I can solve it easily with iteration but using recursion, I think they just want us to have a good handle on it.

The next question is exponents(pos and neg) and then advanced exponents "you will be squaring the results of the base number raised to the power of half of n power." and then fibonacci(#9) and then 9 more problems after that. They get progressively harder.
OriginalGriff 11-Oct-22 5:52am    
Pass the array index instead of removing values - then it looks like the recursive version of factorial.
Chris Aug2022 11-Oct-22 8:55am    
const addToTwelve = array => {
if (array.length < 2) return false;
if (array[0] + array[1] === 12) return true;
return addToTwelve(array.slice(1))
} -----> Bed time
Chris Aug2022 11-Oct-22 5:43am    
What would be the ideal recursive way? Even though, ideally, this would be done iteratively.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900