Code Monkey home page Code Monkey logo

underscore_lab's Introduction

#Underscore Lab

You're going to recreate some of the functionality of the popular JS library Underscore.js. Write all of the following methods inside of an _ object.

Fork this repo, push your code to your fork, and submit the link to your fork on students.gschool

##Part 1

  • average - returns the average value in a given array
_.average([4,6,1,1])
=> 3
  • contains - returns true is a given value is present in an array
_.contains([7,55,99], 99);
=> true
  • first - returns the first element in an array
_.first([5, 4, 3, 2, 1]);
=> 5
  • last - returns the last element in an array
_.last([5, 4, 3, 2, 1]);
=> 1
  • max - returns the maximum value in an array
var numbers = [10, 5, 100, 2, 1000];
_.max(numbers);
=> 1000
  • min - returns the minimum value in an array
var numbers = [10, 5, 100, 2, 1000];
_.min(numbers);
=> 2
  • shuffle - Returns a shuffled copy of the list, using a version of the Fisher-Yates shuffle. Don't worry about implementing that exact shuffle alogrithm. Start by creating your own simple shuffle.
_.shuffle([1, 2, 3, 4, 5, 6]);
=> [4, 1, 6, 3, 5, 2]
  • sample - Produce a random sample from the list. Pass a number to return n random elements from the list. Otherwise a single random item will be returned.
_.sample([1, 2, 3, 4, 5, 6]);
=> 4

_.sample([1, 2, 3, 4, 5, 6], 3);
=> [1, 6, 2]
  • difference - returns the values from array that are not present in the other array.
_.difference([1, 2, 3, 4, 5], [5, 2, 10]);
=> [1, 3, 4]
  • indexOf - Returns the index at which value can be found in the array, or -1 if value is not present in the array.
_.indexOf([1, 2, 3], 2);
=> 1
  • pluck - extracts a list of property values and returns them in an array.
  var stooges = [{name: 'moe', age: 40}, {name: 'larry', age: 50}, {name: 'curly', age: 60}];
_.pluck(stooges, 'name');
=> ["moe", "larry", "curly"]

##Part 2

  • each - iterates over an array and calls a given function with each element
_.each([1, 2, 3], alert);
=> alerts each number in turn...
  • compact - returns a new array with all provided undefined values removed
_.compact([1, "hello", undefined, 3, undefined]);
=> [1, "hello", 3]
  • map - returns a new array of values produced by running each element of an array through a given function
_.map([1, 2, 3], function(num){ return num * 3; });
=> [3, 6, 9]

_.map(["dogs", "before", "cats"], function(str){ return str.toUpperCase(); });
=> ["DOGS", "BEFORE", "CATS"]

  • filter - Looks through each value in the list, returning an array of all the values that pass a truth test
var evens = _.filter([1, 2, 3, 4, 5, 6], function(num){ return num % 2 == 0; });
=> [2, 4, 6]

underscore_lab's People

Contributors

colt avatar dgempler avatar

Watchers

James Cloos avatar  avatar

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.