Tell us what’s happening:
Basic question on how to use console.log() for debugging. I am only able to use in for simple checks. What can I do if I am checking a long return statement? IDK the rule but generally anything more than a simple var in () will not work. any advice on how to better ask this question or where to read to understand this concept will be much appreciated. I will be in your debt.
Your code so far
function steamrollArray(arr) {
function flatDeep(arr, d = 1) {
return d > 0 ? arr.reduce((acc, val) => acc.concat(Array.isArray(val) ? flatDeep(val, d - 1) : val), [])
: arr.slice();
};
var ans = steamrollArray(arr); //also tried steamrollArray([1, [2], [3, [[4]]]]); with the same result.
console.log(ans);
}
steamrollArray([1, [2], [3, [[4]]]]);
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.125 Safari/537.36.
Cactus,
Thats what I am trying to do too but it isn’t working. Can I do a 300hr cert in just console.log? Seriously, its so valuable yet not dependable.
JeremyLT,
What is causing the loop? b/c I called the function inside the function? If I call it outside its just undefined.
Also, the question remains, how to diagnose long return statments. console.log doesn’t like those. I feel very defeated RN
You called the function inside of the function without changing input. You aren’t using flatDeep or modifying arr in any way, so your function is essentially:
You can see the infinite recursion with:
function steamrollArray(arr) {
console.log(arr);
function flatDeep(arr, d = 1) {
return d > 0 ? arr.reduce((acc, val) => acc.concat(Array.isArray(val) ? flatDeep(val, d - 1) : val), [])
: arr.slice();
};
var ans = steamrollArray(arr); //also tried steamrollArray([1, [2], [3, [[4]]]]); with the same result.
console.log(ans);
}
steamrollArray([1, [2], [3, [[4]]]]);
Hang in there, the algorithms section is truly a gauntlet for we mere mortals.
I meant to put the console log outside the body of the function. Just put it inside the parentheses of the function call that’s already there by default.
Of course you can log out whatever you want inside, too.
Once you have the whole function in the console.log, you can move around the return statement and get instant feedback about particular lines.