## Where communities thrive

• Join over 1.5M+ people
• Join over 100K+ communities
• Free without limits
##### Activity
David Castner
@davidjcastner
@kevincauto your algorithm for solving it seems solid, trying to figure out why it gets stuck in a loop
kevincauto
@kevincauto
Thanks so much for looking this over @davidjcastner.
Lily Romano
@Lilyheart
both for starts at -1?
David Castner
@davidjcastner
@Lilyheart it's so he can check for array length, it might be reason for it, let me check something real quick
I've already put together some example code on an alternative method for checking
ah nevermind I've figured it out
Lily Romano
@Lilyheart
/curious
David Castner
@davidjcastner
// for your loop statements use var i = -1 when initializing the index
for(var i = -1; i < arr.length; i++) { /* your loop code */ }
// otherwise i is a global variable and your recursive functions keep overwriting it
// so after it returns from one of them, it goes back to -1 every time

// also I'm typing up an alternative way to check for array length because looping through an array
// starting at index of -1 is confusing and not clear, therefore it's a bad practice
kevincauto
@kevincauto
Oh so i is a global variable that is being reused and causing problems. Putting var in there resets it each time.
David Castner
@davidjcastner
yes and your code works when you use var
Lily Romano
@Lilyheart
Good catch! I totally missed that.
kevincauto
@kevincauto
That's awesome! I just learned something new.
David Castner
@davidjcastner
used a ton of console.log()'s to find it
kevincauto
@kevincauto
Ok so I know that i=-1 was a clunky solution. I just moved the if statement case for empty arrays out of the loop and set the for loop to var i=0 again. It still works.
David Castner
@davidjcastner
Nice exactly what I was going to suggest
kevincauto
@kevincauto
Thank you so much @davidjcastner! That challenge was driving me crazy.
Thanks @Lilyheart for taking a look at it as well!
Lily Romano
@Lilyheart
Man, I gotta get back to FCC soon or both ya’ll gonna pass me!
David Castner
@davidjcastner
Here is what I had typed up for suggestions on how to make your code clearer
function steamrollArray(arr) {
// steamrollArray2 is redundant
// here's how to avoid writing the code twice:
//
// instead we can define a function instead here rather than the global
// namespace and then it avoids writing the same code twice
//
// notice it's doesn't take the answer as an argument
// that's because it will have access to it since it's being defined inside
// the steamrollArray function
var recursiveSteamrollArray = function(arr) {
// starting at index of -1 makes your code unclear, and therefore is
//
// to avoid the case of arr.length being 0 we can check before enterting
// the for loop
if (arr.length === 0) {
}
for (var i = 0; i < arr.length; i++) {
// no need to use if (i === -1) { i++; } anymore
//if the current array index does
//not contain another array
if (!Array.isArray(arr[i])) {
}
//if the current array has a
//sub-array use recursion
if (Array.isArray(arr[i])) {
}
}
};
// recursiveSteamrollArray was defined but not called yet
// so let's call the function and then return answer
recursiveSteamrollArray(arr);
}

console.log(steamrollArray([1, [2],
[3, [
[4]
]]
]));
kevincauto
@kevincauto
Ok cool. I will look this over now.
David Castner
@davidjcastner
@kevincauto it's basically a way to avoid having to duplicate your code in the fcc environment
kevincauto
@kevincauto
@davidjcastner Love it. Yeah that all makes sense. Everything is contained within the function and avoids the global namespace entirely.
Lily Romano
@Lilyheart
Oh that is an interesting way to solve!
David Castner
@davidjcastner
I'm having lunch right now, but you can simplify that even more by using Array.map I'll post that after I eat
Lily Romano
@Lilyheart
Here is how I did it. I am bad at commenting sometimes
function steamrollArray(arr) {
var flatArr = [];
for (var i = 0; i < arr.length; i++) {
if (Array.isArray(arr[i])) {
var tempArr = steamrollArray(arr[i]);
for (var j = 0; j < tempArr.length; j++) {
flatArr.push(tempArr[j]);
}
} else {
flatArr.push(arr[i]);
}
}

return flatArr;
}
kevincauto
@kevincauto
Nice!
David Castner
@davidjcastner
// here is how to do it with Array.map
function steamrollArray(arr) {
var flatten = function(element) {
if (Array.isArray(element)) {
element.map(flatten);
} else {
}
};
flatten(arr);
}
Mike
@dcpking
Slowly working through the BootCamp (for my sins!). Inputs of type date and password are new to me, and placeholders, and also the checking in the button on the form to see if you entered valid dates, etc. I used to have to write JavaScript to validate those!!
Jonathan Herman
@lantech19446
If i learn anything from @davidjcastner it damn well better be commenting my code lol.
Mike
@dcpking
@lantech19446 You're not writing in C now, Jonathan (C == "Write-Only Code")
David Castner
@davidjcastner
@lantech19446 I appreciate the compliment, since if there's anything that I teach to people writing code, it would how to write clean & clear code
Robert Maylie
@rmaylie9560
I definitely wouldn't of thought of writing that method recusively. Although I do try to keep variable scope as small as possible.
Lily Romano
@Lilyheart
Whenever you don't know how deep you will need to go, it is pretty much needed.
Robert Maylie
@rmaylie9560
Can you explain?
Lily Romano
@Lilyheart
Sure! In steamroll, if you didn't have the test cases, you'd have no idea how far deep the arrays go. Maybe 2, Maybe 12. Like in factorial, you may only be doing ^6 or ^106. When writing the code, you don't know how deep you will need to go. Factorial you could for loop, but in steamroll not so much as in factorial you know from the onset how many. But in steamroll, until the code fully dissects the array, you'd never know how many dimensions the array has.
Hopefully that makes sense. Sleep was not so grand.
Robert Maylie
@rmaylie9560
Perfect sense, thank you.
Lily Romano
@Lilyheart
This won't stay running forever, but here is where I'm at in Colt Steele's bootcamp. Registration works (have to register before adding campgrounds or comments). Registration was just added so some things still need tweaking (e.g. author on comment add form). Other things don't work yet and just have placeholders, so no fussin' about that :p Overall really enjoying this. https://colt-steele-bootcamp-lilyheart.c9users.io/campgrounds
David Castner
@davidjcastner
@Lilyheart what are you using for user accounts? I'm curious
Lily Romano
@Lilyheart
You can see ALL the code in the github [Yes, I know even secrets are listed, I'm not really worried about it for this application... and regardless of that, the secret that is pushed isn't the same as the live one anyway :p )
can you create a user and poke around if you wish
David Castner
@davidjcastner
nice, I'll have to look into it
Lily Romano
@Lilyheart
When someone's Facebook account is 5 hours old, I am hesitant to approve...
David Castner
@davidjcastner