GithubHelp home page GithubHelp logo

arrays's Introduction

Array exercises

This is an exercise to help understand array methods in JavaScript. The aim of the project is to add all (or most) of the missing array functions and passing tests.

By implementing the functions you'll get a better understanding as to how arrays work, but more importantly, writing tests for the arrays cements your understanding of how to use the functions.

The implementations do not need to be perfect (i.e. accounting for extreme cases), but you're welcome to add more tests to make your code more robust.

The only requirement is that you do not use any native array methods (i.e. you can't use any of these listed methods).

Setup

First clone (or download) this project, then from the directory, run the following commands. Note that you will need node installed.

git clone [email protected]:remy/arrays.git
cd arrays
npm ci # if this doesn't work, us npm install
npm test -- --watch

Coaching & Pull Request

If you wish to get feedback on your code from someone, feel free to open a pull request on this repo, and in the comment of the pull request issue, make the request for that individual to do a review.

Unless you've been explicitly asked to help, please don't provide unsolicited feedback. I'm sure it's meant with good intentions, but please respect this request.

Tips & Rules

  • Remember that some functions will mutate the array (where the original array is changed inline vs. returning a new array).
  • Do not use any native array methods
  • Use for loops to itterate and execute over arrays
  • For tests, I recommend start with the examples that MDN provide - here's a full list by method
  • Using native array methods is allowed in tests, i.e.
test('indexOf', () => {
  const a = [1, 2, 3, 1];
  expect(array.indexOf(a, 1)).toBe(a.indexOf(1));
})
  • To change the length of an array, you can set the length property, i.e.
const a = [1,2,3];
console.log(a.length); // 3
a.length = 0;
console.log(a.length); // 0

Example

array.push - The push() method adds one or more elements to the end of an array and returns the new length of the array.

Implementation:

function push(array, value) {
  const length = array.length;
  array[length] = value;
  return length + 1;
}

Test:

test('push', () => {
  const a = [1, 2];
  const length = array.push(a, 4);
  expect(length).toBe(3);
  expect(a).toEqual([1, 2, 4]);
});

arrays's People

Contributors

remy avatar

Watchers

 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.