Skip to content

remy/arrays

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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 git@github.com: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]);
});

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published