GithubHelp home page GithubHelp logo

jaanhio / builtin-methods Goto Github PK

View Code? Open in Web Editor NEW

This project forked from wdi-sg/builtin-methods

0.0 3.0 0.0 2 KB

[javascript, morning exercise] Implement some builtin methods in JavaScript

builtin-methods's Introduction

Built-in Methods

This is a related series of simulated technical interview questions, with guiding notes.

Implement each()

Implement a function each(list, action) that applies the action on every item in list:

  • list: input array
  • action: function that determines what to do with an item

To test your implementation, pass in a function that console logs an item as the action argument, and pass in [1, 2, 3] as the list argument, as in: each([1, 2, 3], function () { // ... }). If your implementation is correct, you should see the numbers 1, 2, and 3 printed to Terminal sequentially.

Hint: This is similar to the built-in forEach() method in JavaScript.

Note: This is designed to test if you understand how to pass in a function as a parameter of another function - also known as a callback pattern or callback function (referring to the function being passed in, not the one receiving the function).

Further - implement reduce()

Using your implementation of each(), implement a function that reduces an array to a single value by repetitively invoking a reducer function on each item in the array, and ultimately returns the final value.

The function signature should be:

reduce(list, reducer, accumulator)
  • list: input array
  • reducer: function that determines how to combine a current item's value with the existing cumulative value
  • accumulator: the initial value to start accumulating from

Your reduce() implementation should be able to reduce an array of numbers into a single summed up number (one use case of reduce):

const numbers = [1, 2, 3];
const sum = reduce(numbers, function() { }, 0);

console.log(sum);
//=> 6

Hints:

  • This is very similar to the built-in Array.reduce() in JavaScript - make sure you understand what it's supposed to do before trying to implement it
  • What parameters should the reducer() callback function have?
  • Where does your previously implemented each() function fit in this?
  • Write pseudo-code to layout your approach generally first before coding

Note: This is designed to test how well you understand callbacks and how to make use of them to solve real problems.

Resources

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.