Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Activity
    Robert Maylie
    @rmaylie9560
    Give it a try. satisfy your inner Edison. It probably has a plug in rf module. What frequency range is designated for casual communication?
    spaceballs!!!!!
    Ahhh great movie
    Programming and robotics go together like peas and carrots!
    Robert Maylie
    @rmaylie9560
    I just wish I was smart enough to understand machine learning...
    Jonathan Herman
    @lantech19446
    no such thing as not being smart enough, you just have to figure out how you learn best
    Lily Romano
    @Lilyheart
    @rmaylie9560 I’ve seen this course come recommended https://www.coursera.org/learn/machine-learning
    Also search youtube for a playlist called Machine Learning Summer School 2014. Gitter does weird things to playlist links.
    kevincauto
    @kevincauto
    I'm stuck on the "Steamroller Challenge". I was wondering if someone could take a look at this. Currently, it seems to break the browser when I run it. Infinite loop possibly?
    function steamrollArray(arr) {
       var answer = [];
    
      for(i=-1; i<arr.length; i++){
       //empty array case
        if(arr.length===0){
          return answer;
        }
        if(i===-1){
          i++;
        }
        //if the current array index does 
        //not contain another array
        if(!Array.isArray(arr[i])){
          answer.push(arr[i]);
        }
        //if the current array has a 
        //sub-array use recursion
        if(Array.isArray(arr[i])){
          answer = steamrollArray2(arr[i], answer);
        }
      }
    
      return answer;
    
    }
    //I created a second function just like the first 
    //because I don't want to re-initialize
    //answer = []; and FCC doesn't like global 
    //variables
    function steamrollArray2(arr, answer) {
    
      for(i=-1; i<arr.length; i++){
       //empty array case
        if(arr.length===0){
          return answer;
        }
        if(i===-1){
          i++;
        }
    
        //if the current array index does 
        //not contain another array
        if(!Array.isArray(arr[i])){
          answer.push(arr[i]);
        }
        //if the current array has a sub-array 
        //use recursion to.
        if(Array.isArray(arr[i])){
          steamrollArray2(arr[i],answer);
        }
      }
    
      return answer;
    
    }
    
    steamrollArray([1, [2], [3, [[4]]]]);
    
    /*
     steamrollArray([[["a"]], [["b"]]]) should return ["a", "b"].
    steamrollArray([1, [2], [3, [[4]]]]) should return [1, 2, 3, 4].
    steamrollArray([1, [], [3, [[4]]]]) should return [1, 3, 4].
    steamrollArray([1, {}, [3, [[4]]]]) should return [1, {}, 3, 4]. 
    */
    David Castner
    @davidjcastner
    @kevincauto taking a look at it now
    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
    // use this instead
    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
    your logic is sound though
    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) {
        var answer = [];
        // 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
            // a bad practice, even though your logic is correct
            //
            // to avoid the case of arr.length being 0 we can check before enterting
            // the for loop
            if (arr.length === 0) {
                return answer;
            }
            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])) {
                    answer.push(arr[i]);
                }
                //if the current array has a
                //sub-array use recursion
                if (Array.isArray(arr[i])) {
                    answer = recursiveSteamrollArray(arr[i], answer);
                }
            }
            return answer;
        };
        // recursiveSteamrollArray was defined but not called yet
        // so let's call the function and then return answer
        recursiveSteamrollArray(arr);
        return answer;
    }
    
    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 answer = [];
        var flatten = function(element) {
            if (Array.isArray(element)) {
                element.map(flatten);
            } else {
                answer.push(element);
            }
        };
        flatten(arr);
        return answer;
    }
    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.