FAQ: Code Challenges - Code Challenge 9

This community-built FAQ covers the “Code Challenge 9” exercise from the lesson “Code Challenges”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Web Development

Learn Node-SQLite

FAQs on the exercise Code Challenge 9

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

For Code Challenge 9, my answer below does not pass the check:

db.all(
  'SELECT * FROM Scientist WHERE field = $field',
  { $field: 'biology' },
  (err, rows) => {
    if (err) {
      throw err;
    } else {
      console.log(rows);
    }
  }
);

The solution given is:

db.all("SELECT * FROM Scientist WHERE field='biology'", (err, rows) => {
  console.log(rows);
});

I’m not sure why my solution is considered incorrect.

Similarly in Challenge 13:

db.run(
  'INSERT INTO Bridge (name, established_year) VALUES ($name, $year)',
  {
    $name: 'Brooklyn Bridge',
    $year: 1883
  }
);

Is wrong, but the below is right:

db.run("INSERT INTO Bridge (name, established_year) VALUES ('Brooklyn Bridge', 1883);");

What am I missing?

What’s the name of the variable here?

And what are the names of the attributes in the object you passed along with it?

I have no clue what you’re doing but it looks a whole lot like those don’t match.

A very important consideration is whether it had the desired effect. Don’t rely on codecademy to judge whether it’s right. If it did not have the desired effect then you have a much stronger indication of that it really isn’t right because you put it to the test that matters.

Not sure if I quite understand your follow-up. The variable is 'biology', if I understand what you’re asking. The very first code block I posted is my code, and the second block is the given solution.

But there’s no such property in your object.

Actually, no, that’s not the variable that’s the value that your variable refers to

Sorry, I mean the value of the variable is 'biology'. The variable is $field, as required by the placeholder in the SELECT statement:

db.all(
  'SELECT * FROM Scientist WHERE field = $field',
  { $field: 'biology' },
  (err, rows) => {
    if (err) {
      throw err;
    } else {
      console.log(rows);
    }
  }
);

Are we talking about the same thing?

More likely, the variable’s name doesn’t include the $, typically for string interpolation there’ll be some kind of escape sequence followed by the name of the thing that is special

> this['${stuff}'] = 5 // yeah you can make a variable with that name
5
> stuff = 3  // but more likely it's this name
3
> `${stuff}`
'3'

Yeah, I understand that this is true in general but the format I’ve used is what the library’s .run() function expects:

And you are absolutely right. time for me to get myself a clue then.

Looking at that checkpoint (the condition for it to pass), codecademy is inspecting the structure of your code in a manner that I have nothing kind to say about. They’re not testing the effect of your code, they expect it to match this:

      db.all($query, ($errName, $rowsName) => {
        console.log($rowsName);
      });

…basically they want you to copy paste and have low tolerance for anything else

1 Like

And you are absolutely right. time for me to get myself a clue then.

Haha, all good! We got there in the end.

Looking at that checkpoint…

Out of interest, how’d you find that? Would be good to be able to verify these kinds of things in future.

I was grepping in the source of the exercises but that’s not public.
Sometimes parts of it are copied to the workspace, but that’s not reliable or convenient.

In this case they do copy it there, so taken straight from the learning environment, from test/test.js: (you might have to press run it before the file becomes non-empty…)

console.log = function() {};
const assert = require('chai').assert;
const fs = require('fs');
const Structured = require('structured');

const code = fs.readFileSync('app.js', 'utf8');

describe('', function () {
  it('', function() {
    
    // test use of db.all()
    let dbAllStruct = function() {
    	db.all();
    }
    assert.isOk(Structured.match(code, dbAllStruct), 'Did you use `db.all()` to find a row?');
    
    let queryStruct = function() {
    	db.all($qry);
    }
    
    assert.isOk(Structured.match(code, queryStruct), "Did you pass a query to your `db.all()` call?");
    
    let fnStruct = function() {
    	db.all($query, $cb);
    }
    
    let fnCallbacks = {
    	'$cb': function(cb) {
      	if (cb.type !== 'ArrowFunctionExpression') {
        	return { failure: 'Did you pass an arrow function callback to your `db.all()` call?' }
        }
        return true;
      }
    }

    let fnFailureMessage = "Don't forget to pass a query argument and callback function to `db.all()`"
    
    assert.isOk(Structured.match(code, fnStruct, { varCallbacks: fnCallbacks }), fnCallbacks.failure || fnFailureMessage);
    
    let structure = function() {
      db.all($query, ($errName, $rowsName) => {
      	console.log($rowsName);
      });
    };

    let varCallbacks = {
      '$query': function(query) {
        if (!query.value) {
          return { failure: 'Did you pass a query string to `db.all()`?' } 
        }
        const queryMatch = query.value.match(/SELECT\s*([\s\S]+?)\s+FROM\s+Scientist\s+WHERE\s+field\s*=\s*('|")biology('|")/i);
        if (!queryMatch) {
          return { failure: 'Did you use use a query to select the rows with the `field` of "biology"?' }
        } else {
        	let colName = queryMatch[1];
        if (!colName.match(/\*/)) {
						return { failure: 'Did you select all rows from the Scientist table using *?' }
          }
        }
        return true;
      }
    }

    let isMatch = Structured.match(code, structure, { varCallbacks: varCallbacks });
    let failureMessage = varCallbacks.failure || "Did you pass the correct query into `db.all()` and log the result?";
    assert.isOk(isMatch, failureMessage);
  });
});
1 Like

Thanks @ionatan—that’s still helpful. If I’m totally stuck at least I have a potential way forward.