Improving this recursive

I’ve been struggling hard with recursives & had some inspiration from the “Mutate an Array Declared with const” challenge.

I decided to write something that cracks any array by using recurssion

is there any way I could improve on this code/make it better?

Any help is appreciated!


const needsCracking = [1,2,3,4,5];
const numLeng = 9;




let myArr = []
let x = 0

function outerCrack() {
    
if  (x < needsCracking.length) { 
    
cracker(needsCracking, numLeng);

function cracker(n, i) {
    if (n[x] === i) {
        myArr.push(i);
        x++
        // console.log(myArr)
        outerCrack();
        
    } else { 
        return cracker(n, i - 1);
    }
}
} else {
    console.log("Cracked array:")
    console.log(myArr);
}
} 

outerCrack();


I think for starters you could get rid of the reliance on all those global variables.

2 Likes

So i’ve put some variables inside of the function, is this good practise or is there something i’m missing?

Thanks for the reply!

I’m still like very foundational knowledge, probs a week into learning js from FCC, so hacking my way through it atm

All. Your function should use zero global variables.

1 Like

I’ve updated my comment, should I even be putting the last one in it too?

constNeedsCracking

You should use absolutely zero global variables. That includes needsCracking

1 Like