Skip to content

Michielab/lab-es6-algorithms

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 

Repository files navigation

Ironhack logo

PP | ES6 Algorithms

Introduction

We've taken a look at some of the basic and more advanced features of ES6 in JavaScript. One of the biggest challenges you're going to face going forward is using these new features in the appropriate places.

The easiest way to begin recognizing these patterns is refactoring code that is written in ES5 into ES6.

In this pair-programming challenge, you're going to have snippets of ES5 code. What you will do is refactor this code into the new ES6 standard.

Refactoring Process

Refactoring is hard. It's difficult to fix or update code without accidentally modifying how it works, or breaking some other piece of code that depends on it.

So, what we've done is set up a test suite in mocha to make sure you don't break anything.

![](https://i.imgur.com/jVN0xBn.png =300x)

:::info Mocha is a feature-rich JavaScript test framework running on Node.js and in the browser, making asynchronous testing simple and fun. Mocha tests run serially, allowing for flexible and accurate reporting, while mapping uncaught exceptions to the correct test cases. :::

To get started with Mocha, we must first install it:

$ npm install -g mocha

Then, clone our starter code:

$ git clone https://github.com/ironhack/pp-es6-exercises
$ cd pp-es6-exercises

Once Mocha is installed, we can run our tests by simply entering the mocha command:

$ mocha

  ArrayFunctions
    printSpecial()
      ✓ should return each element with " --- " in between
    doubleArray()
      ✓ should double the value of all numbers in an array
    superPower()
      ✓ should return each element to the power of it's index

  bubbleSort()
    ✓ should an array of numbers sorted from least to greatest

  mergeSort()
    ✓ should an array of numbers sorted from least to greatest

  LetterSequence
    createSequence()
      ✓ should return the sequence with the count of repetitions in between each character
      ✓ should only have a numbered sequence for repeats greater than 1
    decodeSequence()
      ✓ should return a sequence with repetition count in letters


  8 passing (11ms)

The checks mean that all tests are passing.

🐱 If you want to get a fancy output for your tests, try:

$ mocha --reporter nyan

Rules

Do not:

  • change class or function names.
  • modify the line that says module.exports
  • modify or change any of the tests

Do:

  • Run the tests after you modify code. This will tell you what's broken. This can be done by simply running the mocha command.

1. Bubble Sort: bubble-sort.js

Bubble sort is the simplest and slowest sorting algorithm. It simply iterates over the array, and compares each item 2 elements at a time. If the item on the right is less than the item on the left, they will be swapped.

The array will be iterated over until all items are in order.

:::warning ES6 Concepts You Must Use

  • let
  • const :::

2. Merge Sort: merge-sort.js

Merge sort is a more complicated, but often times quicker sorting algorithm.

Merge sort breaks down the array into smaller chunks of 2 to start.

The left and right elements of these pairs of two are compared, and then merged together in sorted order. This continues until we have one final merged array.

:::warning ES6 Concepts You Must Use

  • const / let
  • spread operator :::

3. Array Functions: array-functions.js

In this file, you will find 3 array functions.

3.1 printSpecial

This function takes an input of an array, and turns it into a string seperated with ---.

Example

ArrayFunctions.printSpecial([12, 33, 144, 122])
// => 12 --- 33 --- 144 --- 122

3.2 doubleArray

This function takes an input of an array of numbers, and returns an array with each number doubled (times two)

Example

ArrayFunctions.doubleMyArray([10, 20, 35, 12])
// => [20, 40, 70, 24]

3.3 superPower

superPower takes an array of numbers, and returns a number. This number is created from taking each element multiplied by 10 to the power of it's index position in the array, and then adding each elements together.

Example

ArrayFunctions.superPower([1,2,3,4,5])
// (1 x 10^0) + (2 x 10^1) + (3 x 10^2) + (4 x 10^3) + (5 x 10^4)
// (1)        + (20)       + (300)      + (4000)     + (50000)
// => 54321

:::warning ES6 Concepts You Must Use

4. Letter Sequencer: sequencer.js

LetterSequencer is an object with two methods.

The first function createSequence takes a series of letters, and returns a string with a representation of how many times a particular character occurred in order.

Example

LetterSequence.createSequence("aabbccabbca");
// => 2a2b2ca2bca

LetterSequence.createSequence("aabbcc");
// => "2a2b2c"

LetterSequence.createSequence("aabbbcc");
// => "2a3b2c"

decodeSequence will do the opposite, taking a sequence of letters and numbers from createSequence, giving back a sequence of repeated characters.

Example

LetterSequence.decodeSequence("2a2b2ca2bca");
// => aabbccabbca

LetterSequence.decodeSequence("2a2b2c");
// => aabbcc

LetterSequence.decodeSequence("2a3b2c");
// => aabbbcc

:::warning ES6 Concepts You Must Use

Happy Coding!

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • JavaScript 100.0%