Avatar of brunnock

brunnock's solution

to Matching Brackets in the JavaScript Track

Published at Aug 19 2018 · 0 comments
Instructions
Test suite
Solution

Given a string containing brackets [], braces {}, parentheses (), or any combination thereof, verify that any and all pairs are matched and nested correctly.

Setup

Go through the setup instructions for Javascript to install the necessary dependencies:

https://exercism.io/tracks/javascript/installation

Requirements

Install assignment dependencies:

$ npm install

Making the test suite pass

Execute the tests with:

$ npm test

In the test suites all tests but the first have been skipped.

Once you get a test passing, you can enable the next one by changing xtest to test.

Source

Ginna Baker

Submitting Incomplete Solutions

It's possible to submit an incomplete solution so you can see how others have completed the exercise.

matching-brackets.spec.js

import { isPaired } from './matching-brackets';

describe('Matching Brackets', () => {
  test('paired square brackets', () => {
    expect(isPaired('[]')).toEqual(true);
  });

  xtest('empty string', () => {
    expect(isPaired('')).toEqual(true);
  });

  xtest('unpaired brackets', () => {
    expect(isPaired('[[')).toEqual(false);
  });

  xtest('wrong ordered brackets', () => {
    expect(isPaired('}{')).toEqual(false);
  });

  xtest('wrong closing bracket', () => {
    expect(isPaired('{]')).toEqual(false);
  });

  xtest('paired with whitespace', () => {
    expect(isPaired('{ }')).toEqual(true);
  });

  xtest('partially paired brackets', () => {
    expect(isPaired('{[])')).toEqual(false);
  });

  xtest('simple nested brackets', () => {
    expect(isPaired('{[]}')).toEqual(true);
  });

  xtest('several paired brackets', () => {
    expect(isPaired('{}[]')).toEqual(true);
  });

  xtest('paired and nested brackets', () => {
    expect(isPaired('([{}({}[])])')).toEqual(true);
  });

  xtest('unopened closing brackets', () => {
    expect(isPaired('{[)][]}')).toEqual(false);
  });

  xtest('unpaired and nested brackets', () => {
    expect(isPaired('([{])')).toEqual(false);
  });

  xtest('paired and wrong nested brackets', () => {
    expect(isPaired('[({]})')).toEqual(false);
  });

  xtest('paired and incomplete brackets', () => {
    expect(isPaired('{}[')).toEqual(false);
  });

  xtest('too many closing brackets', () => {
    expect(isPaired('[]]')).toEqual(false);
  });

  xtest('math expression', () => {
    expect(isPaired('(((185 + 223.85) * 15) - 543)/2')).toEqual(true);
  });

  xtest('complex latex expression', () => {
    expect(
      isPaired(
        '\\left(\\begin{array}{cc} \\frac{1}{3} & x\\\\ \\mathrm{e}^{x} &... x^2 \\end{array}\\right)'
      )
    ).toEqual(true);
  });
});
function bracket(string,leftBrace) {
  let [check, ...restOfBraces] = string.split('');
  const pairs = {'(': ')', '[': ']', '{': '}' };
  if (check in pairs) {
    return bracket(restOfBraces.join(''), check);
  } else {
    return(check === pairs[leftBrace])
  }
}
module.exports=bracket;

Community comments

Find this solution interesting? Ask the author a question to learn more.

What can you learn from this solution?

A huge amount can be learned from reading other people’s code. This is why we wanted to give exercism users the option of making their solutions public.

Here are some questions to help you reflect on this solution and learn the most from it.

  • What compromises have been made?
  • Are there new concepts here that you could read more about to improve your understanding?