Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save davidystephenson/a8d6de95e0eda531025121e0b12a43f2 to your computer and use it in GitHub Desktop.

Select an option

Save davidystephenson/a8d6de95e0eda531025121e0b12a43f2 to your computer and use it in GitHub Desktop.
# Questions
## JavaScript
### Question 1
```javascript
async function fetchUser(id) {
const response = await fetch(`/api/users/${id}`);
const user = await response.json();
return user;
}
fetchUser(123).then(user => console.log(user));
```
HWhat could cause the code to fail? What happens if fails? How should the code be modified to handle errors?
### Question 2
```javascript
const arr = [1, 2, 3, 4];
const result1 = arr.map(x => x * 2);
const result2 = arr.splice(1, 2);
console.log(arr);
console.log(result1);
console.log(result2);
```
What will be logged for `arr`, `result1`, and `result2`? Which operations mutate the original array?
## TypeScript
### Question 2
```typescript
function getProperty<T, K extends keyof T>(obj: T, key: K): T[K] {
return obj[key];
}
const user = { name: "Alice", age: 30 };
const name = getProperty(user, "name");
const invalid = getProperty(user, "email");
```
Will this code compile? What does `K extends keyof T` mean?
### Question 1
```typescript
function processValue(value: string | number | boolean) {
if (typeof value === "string") {
return value.toUpperCase();
}
return value.toFixed(2);
}
```
Will this code compile? What type does `value` have on each line?
## Database
### Question 1
```typescript
async function logPosts() {
const users = await db.select().from(usersTable).where(eq(usersTable.role, 'admin'));
for (const user of users) {
const posts = await db.select().from(postsTable).where(eq(postsTable.userId, user.id));
console.log('${user.name} posts:', posts);
}
}
```
How many database queries does this approach make if there are 10 admin users? How would you implement this function?
### Question 3: Transactions
```typescript
await db.transaction(async (tx) => {
await tx.update(accountsTable)
.set({ balance: sql`balance - 100` })
.where(eq(accountsTable.id, 1));
await tx.update(accountsTable)
.set({ balance: sql`balance + 100` })
.where(eq(accountsTable.id, 2));
});
```
What happens if the second update fails? When is this behavior desirable or undesirable?
## API
### Question 1
```javascript
app.post('/api/users', async (req, res) => {
const user = await createUser(req.body);
res.json(user);
});
```
What status code should this endpoint return? What cases should it return different status codes?
### Question 2
```javascript
app.get('/api/users/search', async (req, res) => {
const searchTerm = req.query.name;
const users = await db.execute(
sql`SELECT * FROM users WHERE name = '${searchTerm}'`
);
res.json(users);
});
```
How would a hacker attack this enpdoint? What is the simplest way to fix it?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment