Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 19 additions & 5 deletions 01-basic-cs/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,29 @@ const assert = require('assert')

const database = require('./database.json')

const total = 0 // TODO
const getTotalSales = () => {
const hats = _.flatMap(database, 'hats')
const hatSales = _.countBy(hats, 'id')
const sortHats = _.orderBy(
_.keys(hatSales),
(hatId) => hatSales[hatId],
'desc'
)
const top3Hats = _.slice(sortHats, 0, 3)
const totalSales = _.sum(_.map(top3Hats, (hatId) => hatSales[hatId]))

// Throws error on failure
return totalSales
}

const total = getTotalSales()

// Throws an error if the total is not equal to 23
assert.equal(total, 23, `Invalid result: ${total} != 23`)

console.log('Success!')
console.log(`The total sales of the top-3 hats is: ${total} hats`)

/**
* Time and space complexity in O() notation is:
* - time complexity: TODO
* - space complexity: TODO
* - time complexity: O(n log n)
* - space complexity: O(n + d)
*/
20 changes: 17 additions & 3 deletions 02-nodejs/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,23 @@ const express = require('express')

const User = require('./models/User')

// Setup Express.js app
const app = express()
app.get('/', (req, res) => {
res.send("Please use the following route to download CSV file: '/users'")
})
app.get('/users', (req, res) => {
const cursor = User.find().cursor()
res.setHeader('Content-Type', 'text/csv')
res.setHeader('Content-Disposition', 'attachment; filename="users.csv"')
res.write('name,email\n')

// TODO: everything else
cursor.on('data', (user) => {
res.write(`${user.name},${user.email}\n`)
})

app.listen(3000)
cursor.on('end', () => {
res.end()
})
})

app.listen(3001, () => console.log('listen'))
1 change: 1 addition & 0 deletions 03-react/src/App.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import React from 'react'
import { BrowserRouter, Route, Switch } from 'react-router-dom'
import Exercise from './components/pages/Exercise'

Expand Down
155 changes: 99 additions & 56 deletions 03-react/src/components/pages/Exercise/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable react/react-in-jsx-scope */
import './assets/styles.css'
import { useState } from 'react'

Expand Down Expand Up @@ -49,65 +50,107 @@ export default function Exercise01 () {
}
])

const getTotal = () => 0 // TODO: Implement this
const addToCart = (movie) => {
const existingMovie = cart.find((item) => item.id === movie.id)
if (existingMovie) {
setCart((prevCart) =>
prevCart.map((item) =>
item.id === movie.id ? { ...item, quantity: item.quantity + 1 } : item
)
)
} else {
setCart((prevCart) => [
...prevCart,
{
id: movie.id,
name: movie.name,
price: movie.price,
quantity: 1
}
])
}
}

const decrementQuantity = (item) => {
const updatedCart = cart.map((x) => {
if (x.id === item.id) {
const updatedQuantity = x.quantity - 1
if (updatedQuantity === 0) {
return null
}
return {
...x,
quantity: updatedQuantity
}
}
return x
}).filter(Boolean)
setCart(updatedCart)
}

const incrementQuantity = (movie) => {
setCart((prevCart) =>
prevCart.map((item) =>
item.id === movie.id ? { ...item, quantity: item.quantity + 1 } : item
)
)
}

const getTotal = () => {
const total = cart.reduce(
(accumulator, item) => accumulator + item.price * item.quantity,
0
)

const applicableDiscounts = discountRules.filter((rule) =>
rule.m.every((movieId) => cart.some((item) => item.id === movieId))
)

const discountAmount = applicableDiscounts.reduce(
(accumulator, rule) => accumulator + total * rule.discount,
0
)

return total - discountAmount
}

return (
<section className="exercise01">
<div className="movies__list">
<ul>
{movies.map(o => (
<li className="movies__list-card">
<ul>
<li>
ID: {o.id}
</li>
<li>
Name: {o.name}
</li>
<li>
Price: ${o.price}
</li>
</ul>
<button onClick={() => console.log('Add to cart', o)}>
Add to cart
</button>
</li>
))}
</ul>
</div>
<div className="movies__cart">
<ul>
{cart.map(x => (
<li className="movies__cart-card">
<ul>
<li>
ID: {x.id}
</li>
<li>
Name: {x.name}
</li>
<li>
Price: ${x.price}
</li>
</ul>
<div className="movies__cart-card-quantity">
<button onClick={() => console.log('Decrement quantity', x)}>
-
</button>
<span>
{x.quantity}
</span>
<button onClick={() => console.log('Increment quantity', x)}>
+
</button>
</div>
</li>
))}
</ul>
<div className="movies__cart-total">
<p>Total: ${getTotal()}</p>
</div>
<div className="movies__list">
<ul>
{movies.map((movie) => (
<li className="movies__list-card" key={movie.id}>
<ul>
<li>ID: {movie.id}</li>
<li>Name: {movie.name}</li>
<li>Price: ${movie.price}</li>
</ul>
<button onClick={() => addToCart(movie)}>Add to cart</button>
</li>
))}
</ul>
</div>
<div className="movies__cart">
<ul>
{cart.map((item) => (
<li className="movies__cart-card" key={item.id}>
<ul>
<li>ID: {item.id}</li>
<li>Name: {item.name}</li>
<li>Price: ${item.price}</li>
</ul>
<div className="movies__cart-card-quantity">
<button onClick={() => decrementQuantity(item)}>-</button>
<span>{item.quantity}</span>
<button onClick={() => incrementQuantity(item)}>+</button>
</div>
</li>
))}
</ul>
<div className="movies__cart-total">
<p>Total: ${getTotal()}</p>
</div>
</section>
</div>
</section>
)
}
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@
"express": "^4.14.1",
"isomorphic-fetch": "^2.2.1",
"lodash": "^4.17.4",
"moment": "^2.17.1",
"mongoose": "^4.8.4",
"mongodb": "^5.6.0",
"mongoose": "^7.3.1",
"morgan": "^1.8.1"
},
"devDependencies": {
Expand Down