Yana P.#2
Conversation
📝 HackYourFuture auto gradeAssignment Score: 0 / 100 ✅Status: ✅ Passed Test Details |
remarcmij
left a comment
There was a problem hiding this comment.
Hi @YanaP1312, your queries from task 1 look good. In task 2, some functions do not completely mirror what the JSON versions do. Also, take note how boolean values should be treated.
| SELECT title, published_year FROM books | ||
| WHERE genre = "Science Fiction" | ||
| ORDER BY published_year ASC; | ||
|
|
There was a problem hiding this comment.
Note that single quotes are preferred in SQL for string literals.
|
|
||
| SELECT title, published_year FROM books | ||
| WHERE published_year < 1950 | ||
| ORDER BY published_year DESC; |
There was a problem hiding this comment.
Fine, but no explicit order was asked for.
|
|
||
| SELECT b.title, b.published_year, b.genre, a.first_name || " " || a.last_name AS author FROM books b | ||
| JOIN authors a ON a.id=b.author_id | ||
| ORDER BY author, published_year; |
There was a problem hiding this comment.
No order was asked for, but your chosen order makes perfect sense.
|
|
||
| -- **Question 4** — List all books written by Stephen King. Show the title and published year, ordered by year. _(Hint: JOIN the two tables and filter on the author's name.)_ | ||
|
|
||
| SELECT b.title, b.published_year, a.first_name || " " || a.last_name AS author FROM books b |
|
|
||
| SELECT b.title, b.published_year, a.first_name || " " || a.last_name AS author FROM books b | ||
| JOIN authors a ON a.id=b.author_id | ||
| WHERE a.first_name || " " || a.last_name = "Stephen King" |
There was a problem hiding this comment.
It is not necessary to create the full name for the WHERE clause.
| WHERE a.first_name || " " || a.last_name = "Stephen King" | |
| WHERE a.first_name = 'Stephen' AND a.last_name = 'King' |
| const existedCardsInfoForDeck = getAllCardsForDeck(deckId); | ||
|
|
||
| const existing = existedCardsInfoForDeck.find( | ||
| (card) => card.question === question && card.answer === answer | ||
| ); | ||
|
|
||
| if (existing) { | ||
| return existing; | ||
| } |
There was a problem hiding this comment.
The JSON version does not check for an existing card. If you want to do this, try and SELECT a single card (imagine there may be millions of cards: you dont want to get them all just so you can filter them in JS).
| return db | ||
| .prepare(`SELECT id, question FROM cards WHERE id = ?`) | ||
| .get(result.lastInsertRowid); |
There was a problem hiding this comment.
Alternatively, you can construct an object yourself, without the need to hit the database again:
| return db | |
| .prepare(`SELECT id, question FROM cards WHERE id = ?`) | |
| .get(result.lastInsertRowid); | |
| return { | |
| id: Number(info.lastInsertRowid), | |
| question, | |
| answer, | |
| learned: false, | |
| deckId, | |
| }; |
| // TODO: set learned = 1 for the card with the given id | ||
| // return the updated row, or null if not found | ||
| throw new Error('Not implemented'); | ||
| db.prepare(`UPDATE cards SET learned = 1 WHERE id=?`).run(cardId); |
There was a problem hiding this comment.
The JSON version returns null if the card is not found.
| db.prepare(`UPDATE cards SET learned = 1 WHERE id=?`).run(cardId); | |
| const info = db | |
| .prepare("UPDATE cards SET learned = 1 WHERE id = ?") | |
| .run(cardId); | |
| if (info.changes === 0) { | |
| return null; | |
| }``` |
| return { | ||
| id: card.id, | ||
| learned: card.learned === 1 ? 'true' : 'false', | ||
| }; |
There was a problem hiding this comment.
- The JSON version returns a complete car object (with
learnedas a boolean and adeckIdinstead ofdeck_id). trueandfalseare boolean literals, not strings.
| // return true if a row was deleted, false otherwise | ||
| throw new Error('Not implemented'); | ||
| const card = db.prepare(`DELETE FROM cards WHERE id=?`).run(cardId); | ||
| return card.changes > 0 ? 'true' : 'false'; |
There was a problem hiding this comment.
| return card.changes > 0 ? 'true' : 'false'; | |
| return card.changes > 0; |
No description provided.