🎉 Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io 🎉
Avatar of luke13579

luke13579's solution

to Proverb in the JavaScript Track

Published at Sep 18 2020 · 0 comments
Instructions
Test suite
Solution

For want of a horseshoe nail, a kingdom was lost, or so the saying goes.

Given a list of inputs, generate the relevant proverb. For example, given the list ["nail", "shoe", "horse", "rider", "message", "battle", "kingdom"], you will output the full text of this proverbial rhyme:

For want of a nail the shoe was lost.
For want of a shoe the horse was lost.
For want of a horse the rider was lost.
For want of a rider the message was lost.
For want of a message the battle was lost.
For want of a battle the kingdom was lost.
And all for the want of a nail.

Note that the list of inputs may vary; your solution should be able to handle lists of arbitrary length and content. No line of the output text should be a static, unchanging string; all should vary according to the input given.

Setup

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

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

Requirements

Please cd into exercise directory before running all below commands.

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.

Submitting Solutions

Once you have a solution ready, you can submit it using:

exercism submit proverb.js

Submitting Incomplete Solutions

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

Exercise Source Credits

Wikipedia http://en.wikipedia.org/wiki/For_Want_of_a_Nail

proverb.spec.js

import { proverb } from './proverb';

describe('Proverb Test Suite', () => {
  test('a single consequence', () => {
    const result = proverb('nail', 'shoe');

    expect(result).toEqual(
      `For want of a nail the shoe was lost.
And all for the want of a nail.`,
    );
  });

  xtest('a short chain of consequences', () => {
    const result = proverb('nail', 'shoe', 'horse');

    expect(result).toEqual(
      `For want of a nail the shoe was lost.
For want of a shoe the horse was lost.
And all for the want of a nail.`,
    );
  });

  xtest('a longer chain of consequences', () => {
    const result = proverb('nail', 'shoe', 'horse', 'rider');
    expect(result).toEqual(
      `For want of a nail the shoe was lost.
For want of a shoe the horse was lost.
For want of a horse the rider was lost.
And all for the want of a nail.`,
    );
  });

  xtest('proverb function does not hard code the rhyme dictionary', () => {
    const result = proverb('key', 'value');

    expect(result).toEqual(
      `For want of a key the value was lost.
And all for the want of a key.`,
    );
  });

  xtest('the whole proveb', () => {
    const result = proverb('nail', 'shoe', 'horse', 'rider',
      'message', 'battle', 'kingdom');

    expect(result).toEqual(
      `For want of a nail the shoe was lost.
For want of a shoe the horse was lost.
For want of a horse the rider was lost.
For want of a rider the message was lost.
For want of a message the battle was lost.
For want of a battle the kingdom was lost.
And all for the want of a nail.`,
    );
  });

  xtest('proverb is the same each time', () => {
    expect(proverb('nail', 'shoe')).toEqual(proverb('nail', 'shoe'));
  });

  xtest('the use of an optional qualifier in the final consequence', () => {
    const result = proverb('nail', 'shoe', 'horse', 'rider',
      'message', 'battle', 'kingdom',
      { qualifier: 'horseshoe' });

    expect(result).toEqual(
      `For want of a nail the shoe was lost.
For want of a shoe the horse was lost.
For want of a horse the rider was lost.
For want of a rider the message was lost.
For want of a message the battle was lost.
For want of a battle the kingdom was lost.
And all for the want of a horseshoe nail.`,
    );
  });
});
export const proverb =(...args)=>{//use rest arguments to anticipate an unknown number of arguments.
  let phrase =[]//creates an array to store each new line of the proverb.
  let qualifier = args[args.length-1].qualifier;//the user can specify a qualifier argument using destructuring. This just stores that value.
  
  /*
   -n stores the number of arguments in array minus one. This is because the way the algorithim is setup for the problem.
   -There are repeated lines, the ones that start with "For want of..." and the final line which begins with "And all for the want of..."
   -There is always 1 less repeated line than there are function arguments. That's where the -1 comes in. 
   -If the arguments include a qualifier it's obviously not supposed to be apart of the repeated lines. Therefore, the if statement checks for a qualifier. If found it -2 from n to account for it. 
  */
  let n = args.length-1;
  if(!qualifier) {
    qualifier = "";//make qualifier blank by default if it doesn't exist.
  } else {
    n = args.length-2;
    qualifier += " ";//if it does exist add space back in front of it so it fits in with the final line.
  }

  
  let finalPhrase = `And all for the want of a ${qualifier}${args[0]}.`//programming for the final line of the proverb. It's slightly different from the rest in structure so it's dealt with here.

  for(let i=0;i<n; i++) {//loop through all of the phrases according to how many arguments (n) are given. 
      phrase.push(`For want of a ${args[i]} the ${args[i+1]} was lost.
`)//This just says for each line take an argument and then the next argument and insert those into the poem. You'll see why if you look at the pattern of the proverb. Notice the line break. Strings encased in backticks follow literal formatting. It won't compress and autoformat like "" or ''.
  }
  
  let rv = phrase.join(``)+finalPhrase//rv is  `return value`. This concatenates all of the repeated lines generated from the loop with the final line which is outside of the loop. String.join(p) turns an array into a string with p being the seperator. In this case it was just a space. So all of the lines of the poem are returned with spaces in between each line. Also remember the line break that's added in with the broken line on 28-29
  return(rv);//finally return the `return value` or rv.

}

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?