Interview Questions

  • View all interview questions
  • blank
  • Map, reduce, and filter - JavaScript functional programming
    Three functions that are commonly used when applying functional programming techniques in Javascript are the map, reduce, and filter functions. We'll go over each below and explain how they work.


    You need to be a premium member to see the rest of this question and code.

    Become a Premium Member

    With our large collection of challengs, tutorials, and solutions, we make it easy for you to become a better coder, prepare for interviews, and learn new skills from more experienced coders.

    “Daniel - Thanks so much for making Coderbyte! We still recommend it to people.” ― Shawn Drost
    • 200+ Coding Challenges
    • Mock Interview Questions
    • 500,000+ Code Solutions
    • Algorithm Tutorials
    • Interview Prep Courses
    mrdaniel published this on 4/12/16 | javascript, array, map, reduce, filter, App Academy, Codesmith, Fullstack Academy, Hack Reactor
    Comments
  • +
  • 7
  • -
  • I have a question regarding the filter function above. In the if statement it tests to make sure that el.name and el.age are not undefined, but why does it filter out the el.name = 'robert' / el.age = null object? I suspect it is because el.age = null, but I guess I just don't understand why it would consider null as undefined in the el.age != undefined statement.
    if (el.name != undefined && el.age != undefined) {  
        return true;
    }
    
  • +
  • 0
  • -
  • I have a question, why in the Map section, in the second example the choice to round a number is the function Math.ceil()
     
    var rounded = arr.map(Math.ceil);
    // instead of rounding it by using
    var rounded = arr.map(Math.round);
    
    Why not using Math.round(), which gives back a more accurate mathematical round result, where 5.1 would be 5 and not 6? Just a thought. not saying is wrong.
  • +
  • 0
  • -
  • @connorch, that element is filtered out because in JavaScript, null == undefined (because of the double equal, type coercion takes place). If, on the other hand, we tested by doing:
    if (el.name !== undefined && el.age !== undefined) {
      return true;
    }
    then that element would not be filtered out.
  • +
  • 0
  • -
  • @mrdaniel, Ooh that makes a lot of sense. (null == undefined) is true, but (null === undefined) is false. Got it. Thanks for your help!
    Login to submit a comment