Gold Medal Metrics: why does not my 'bestYear' function pass the test?

Hi there! I’m working on the Gold Medal Metrics project, and can´t figure out why my code does not pass the test. I have taken a look at the solution code, that basically does the same thing, although in a different way. Any clarification would be much appreciated! Thanks in advance!

Project: https://www.codecademy.com/paths/web-development/tracks/sql-for-web-development/modules/project-gold-medal-metrics/informationals/bapi-p5-gold-medal-metrics

Task description:

bestYear

Takes an argument, the name of a country. Returns the SQL command that will retrieve the year that country won the most Olympic gold medals, and how many medals were won, aliased to the name count .

My code:

const bestYear = country => {
  return `SELECT year, COUNT(*) AS count FROM GoldMedal WHERE country = '${country} GROUP BY 1 ORDER BY 2 DESC LIMIT 1';`;
};

Test output:

Solution Code:

const countryBestWithCount = (bestThing, country) => {
  if (['year', 'discipline', 'sport', 'event'].includes(bestThing)) {
    return `SELECT ${bestThing}, COUNT(*) AS count FROM GoldMedal WHERE country = '${country}' GROUP BY ${bestThing} ORDER BY COUNT(*) DESC LIMIT 1;`;
  }
  return null;
};

/*
Returns a SQL query string that will find the year where the given country 
won the most medals, along with the number of medals aliased to 'count'.
*/

const bestYear = country => {
  return countryBestWithCount('year', country);

Note, that the solution code makes use of an additional function ( countryBestWithCount), but the final query is the same.

Thanks again!

That’s only part of the output, what you omitted says what’s wrong about the result, and you can look at the test code to determine what the input was/information about how to reproduce it if you need to inspect it yourself.

how are you measuring that?

One way to measure would be to write them both out and compare them

SELECT year, COUNT(*) AS count FROM GoldMedal WHERE country = 'Paraguay GROUP BY 1 ORDER BY 2 DESC LIMIT 1';
SELECT year, COUNT(*) AS count FROM GoldMedal WHERE country = 'Paraguay' GROUP BY year ORDER BY COUNT(*) DESC LIMIT 1;

same?

First and foremost thanks for your

Short answer: yes, they give the same results.

Longer answer:

Result 1 (I used ‘Sweden’ instead of ‘Paraguay’ to actually get some results):

Result 2:

So either I need glasses, or the results are the same :slight_smile:

Looking forward to your reply. Cheers

That’s not what your function returns. You wrote this separately, you did not obtain it from your function. In the process, you fixed it.

What you would want to do is call both functions with 'Paraguay' as the input, copy the results, and run those queries against the same database as the test used.

If you start changing a bunch of things … then you’re no longer looking at the same situation.

You’d still probably find the problem with ‘Sweden’ and this other database, because those are not the changes that fixes the problem, there’s something else as well. But in principle, the less you change the better, ideally use the same environment as the failure is in. (Avoid observing what you think it does, observe the thing itself)

1 Like

Sagely advice. Thank you.

Thanks again for your reply!

Yes, you are absolutely right! I put on my glasses, took a closer look at the query and found a misplaced single quotation mark… now the tests pass, thank you sir!

I have learned (once more) the importance of triple-checking the code for any misplaced characters. And I will try to find a Linter that catches these types of badly placed quotation marks.

The strange thing with Paraguay, that is used as the test country in the test script, is that they have not won any medals at all, according to the database used in the project:

Test script using ‘Paraguay’ as test country:

describe('bestYear', function() {
  it('a function called bestYear exists', function() {
    expect(sql.bestYear).to.exist;
    expect(typeof sql.bestYear).to.equal('function');
  });

  it('should return a SQL query string that retrieves the year a country won the most gedals', function(done) {
    const query = sql.bestYear('Paraguay');
    seededDb.get(query, (error, result) => {
      let year = result && result.year;
      if (year === 1961) {
        done();
      } else {
        done(new Error(error || `Expected year to be 1961, actual value was ${year}`));
      }
    });
  });

  it('should return a SQL query string that retrieves the number of medals a country won in their best year', function(done) {
    const query = sql.bestYear('Paraguay');
    seededDb.get(query, (error, result) => {
      let count = getCount(result);
      if (count === 2) {
        done();
      } else {
        done(new Error(error || `Expected count to be 2, actual value was ${count}`));
      }
    });
  });
});

If I understand the test script correctly, Paraguay is used as a test country and the expected year they won the most amount of medals is ‘1961’, and the expected value for amount of medals won is ‘2’.

But how come that the test script passes, taking into consideration that Paraguay has not won any medals at all, according to my query in the database?

Thanks in advance for all the help!

A linter wouldn’t find it because it’s part of a string.
And triple checking stuff is hard! But since there’s a crash or error of some sort, there’s something observable, and putting the two versions next to each other makes them comparable and then the quote stands out.
Running the resulting query probably results in some error or such as well, or maybe some sql linter, but it’s a computed string so…yeah.

if you look at the test code you can see what database is used.

Yes, that was the source of the confusion - the test script does not query the same database as the one used for completing the project.

Thanks again for all the input!

This topic was automatically closed 18 hours after the last reply. New replies are no longer allowed.