Giter Club home page Giter Club logo

advanced-iterators's Introduction

Interview Prep - Iterators

Today you'll be implementing iterator functions on the whiteboard in pairs or threes. As you work through, alternate who is the "candidate" for each problem. The candidate's job will be to convince the other person, the "interviewer," that they've solved the problem. The "interviewer" can look at the solution and give hints if needed.

Note: DO NOT use any built-in iterator functions. You will often be asked in interviews to implement well-known methods like this from scratch as problem-solving exercises.

How to Get Started

  • Use pseudo-code to plan the logic of your function before writing actual code on the board.

  • Write down at least one new example of test input, and write down the expected output. Hint: For many iterators, one of your test inputs will be a function.

  • Only when you have pseudocode and test input with expected output should you write code to implement the body of the function.

Problems

  1. each(arr, callback)

    function exclaim(phrase){
    	console.log(phrase + "!");
    }
    
    each(["hi", "what?"], exclaim);
    
    // hi!
    // what?!
    // => undefined

Write a function called each that takes in an array and a callback function. each should iterate through all items in the array and call the callback function with each item as a parameter. each should return undefined.

  1. map(arr, callback)

    function question(phrase){
    	return phrase + "?";
    }
    
    map(["who", "you"], question);
    
    // => ["who?", "you?"]

    Write a function called map that takes in an array and a callback function. map should iterate through all items in the array, call the callback function with each item and its index as parameters, and return a new array containing all the values returned by the callback.

  2. filter(arr, callback)

    function isEven(num){
    	return num % 2 === 0;
    }
    
    filter([1, 2, 3, 4, 5, 6], isEven);
    
    // => [2, 4, 6]

    Write a function called filter that takes in an array and another function (which will return a boolean). filter should iterate through the array, check whether each the callback function returns true for each value, and return a new array containing all the values that did return true.

  3. partition(arr, callback)

    function isOdd(num){
    	return num % 2 !== 0;
    }
    
    partition([0, 1, 2, 3, 4, 5], isOdd);
    // => [[1, 3, 5], [0, 2, 4]]

    Write a function called partition that takes in an array and a callback function. partition should split the array into two arrays: one with all the elements for which the callback returned true, and one with all the elements for which the callback returned false. It should return a new array with the two partitioned arrays nested inside.

  4. pluck(arr, key)

    grandparents = [
      {first: "June", last: "Crane", age: 74},
      {first: "Jim", last: "Crane", age:76},
      {first: "Linda", last: "Fuentes", age: 62},
      {first: "Panfilo", last: "Fuentes", age: 76}
      ];
    
    pluck(grandparents, 'first');
    // =>["June", "Jim", "Linda", "Panfilo"]

    Write a function called pluck that takes in an array of objects and a key. pluck should iterate through the array, pick out the value each object has associated with the given key, and return a new array containing those values.

  5. where(arr, properties)

events = [ {location: "Yerba Buena", day: "Wednesday", time: "0900"}, {location: "GA", day: "Tuesday", time: "1830"}, {location: "Blue Bottle", day: "Tuesday", time: "1100"}, {location: "GA", day: "Thursday", time: "1830"}, {location: "GA", day: "Thursday", time: "0917"} ];

where(events, {time: "1830", location: "GA"});
// => [
//			{location: "GA", day: "Tuesday", time: "1830"},
//			{location: "GA", day: "Thursday", time: "1830"}
//	  ]
```


Write a function called `where` that takes an array of objects and another object. `where` looks through each object in the array and returns a new array containing the objects that match *every* key-value pair given in the second argument.
  1. isEqual(obj1, obj2)

thing1 = { flavor: "mango", nutritionInformation: { calories: 80, sugar: 2, servingSize: "8 oz" } };

thing2 = {
	flavor: "mango",
	nutritionInformation: {
		calories: 80,
		sugar: 2,
		servingSize: "8 oz"
	}
};

isEqual(thing1, thing2);
// => true

thing1 == thing1;
// => true

thing1 == thing2;
// => false
```


Write a function called `isEqual` that takes in two objects. `isEqual` compares the objects based on the _values_ of all of the keys and values inside. (Why is this different from normal `==` behavior?)  It returns `true` if the objects have all the same keys and matching values, even if they're not stored in the same memory location. It returns false if not. Note that a solution is not provided in this repo.  Feel free to check out lodash's [source code for `baseIsEqual`](https://github.com/lodash/lodash/blob/4.16.4/lodash.js#L3266).

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.