Avatar of brunnock

brunnock's solution

to Allergies in the JavaScript Track

Published at Jul 13 2018 · 0 comments
Test suite


This solution was written on an old version of Exercism. The tests below might not correspond to the solution code, and the exercise may have changed since this code was written.

Given a person's allergy score, determine whether or not they're allergic to a given item, and their full list of allergies.

An allergy test produces a single numeric score which contains the information about all the allergies the person has (that they were tested for).

The list of items (and their value) that were tested are:

  • eggs (1)
  • peanuts (2)
  • shellfish (4)
  • strawberries (8)
  • tomatoes (16)
  • chocolate (32)
  • pollen (64)
  • cats (128)

So if Tom is allergic to peanuts and chocolate, he gets a score of 34.

Now, given just that score of 34, your program should be able to say:

  • Whether Tom is allergic to any one of those allergens listed above.
  • All the allergens Tom is allergic to.

Note: a given score may include allergens not listed above (i.e. allergens that score 256, 512, 1024, etc.). Your program should ignore those components of the score. For example, if the allergy score is 257, your program should only report the eggs (1) allergy.


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


Running the test suite

The provided test suite uses Jasmine. You can install it by opening a terminal window and running the following command:

npm install -g jasmine

Run the test suite from the exercise directory with:

jasmine allergies.spec.js

In many test suites all but the first test have been marked "pending". Once you get a test passing, activate the next one by changing xit to it.


Jumpstart Lab Warm-up http://jumpstartlab.com

Submitting Incomplete Solutions

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


var Allergies = require('./allergies');

describe('Allergies', function () {
  it('no allergies at all', function () {
    var allergies = new Allergies(0);

  xit('allergies to eggs', function () {
    var allergies = new Allergies(1);
    expect(allergies.list()).toEqual([ 'eggs' ]);

  xit('allergies to peanuts', function () {
    var allergies = new Allergies(2);
    expect(allergies.list()).toEqual([ 'peanuts' ]);

  xit('allergies to strawberries', function () {
    var allergies = new Allergies(8);
    expect(allergies.list()).toEqual([ 'strawberries' ]);

  xit('allergies to eggs and peanuts', function () {
    var allergies = new Allergies(3);
    expect(allergies.list()).toEqual([ 'eggs', 'peanuts' ]);

  xit('allergies to more than eggs but not peanuts', function () {
    var allergies = new Allergies(5);
    expect(allergies.list()).toEqual([ 'eggs', 'shellfish' ]);

  xit('allergic to lots of stuff', function () {
    var allergies = new Allergies(248);
    expect(allergies.list()).toEqual(['strawberries', 'tomatoes', 'chocolate', 'pollen', 'cats']);

  xit('allergic to everything', function () {
    var allergies = new Allergies(255);
    expect(allergies.list()).toEqual(['eggs', 'peanuts', 'shellfish', 'strawberries', 'tomatoes', 'chocolate', 'pollen', 'cats']);

  xit('no allergic means not allergic', function () {
    var allergies = new Allergies(0);

  xit('allergic to eggs', function () {
    var allergies = new Allergies(1);

  xit('allergic to eggs and other things', function () {
    var allergies = new Allergies(5);

  xit('ignore non allergen score parts', function () {
    var allergies = new Allergies(509);
    expect(allergies.list()).toEqual(['eggs', 'shellfish', 'strawberries', 'tomatoes', 'chocolate', 'pollen', 'cats']);
function Allergies(score){

  var allergies = 'eggs peanuts shellfish strawberries tomatoes chocolate pollen cats'.split(' ');

  var score_arr = score.toString(2).split('').reverse().slice(0,8);
  this.list = function() {
    var list=[];

    for (var x=0; x<score_arr.length; ++x) {
      if (score_arr[x]==1) list.push(allergies[x]);

    return list;

  this.allergicTo = function(allergen) {
      return Boolean(score_arr[allergies.indexOf(allergen)]);


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?