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
16 changes: 15 additions & 1 deletion 01-basic-cs/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,12 @@ const assert = require('assert')

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

const total = 0 // TODO
const hats = _.flatMap(database, 'hats')
const obtainIds = _.countBy(hats, 'id')
const separatePairsIds = _.toPairs(obtainIds)
const orderDesc = _.orderBy(separatePairsIds, ([idHat, count]) => count, 'desc')
const firstThreeElements = orderDesc.slice(0, 3)
const total = _.sumBy(firstThreeElements, ([hatId, count]) => count)

// Throws error on failure
assert.equal(total, 23, `Invalid result: ${total} != 23`)
Expand All @@ -16,4 +21,13 @@ console.log('Success!')
* Time and space complexity in O() notation is:
* - time complexity: TODO
* - space complexity: TODO
* With the resolution of the problem, the following is obtained:
Time complexity: O(n + m log m)
space complexity: O(n + m), where n is the total number of hats sold and m is the total number of different hats sold.
Where:
n: total number of hats sold and
m: total number of different hats sold.
therefore:
n = 117 -> Total hats sold
m = 28 -> Total number of different hats sold
*/
15 changes: 15 additions & 0 deletions 02-nodejs/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,20 @@ const User = require('./models/User')
const app = express()

// TODO: everything else
app.get('/', async (req, res) => {
const users = await User.find()
const keys = Object.keys(users[0])
const dataCsv = users.map(user => keys.map(key => user[key]))
res.setHeader('Content-Type', 'text/csv')
res.setHeader('Content-Disposition', 'attachment; filename="users.csv"')
res.write('\ufeff')
res.write(keys.join(','))
res.write('\n')
dataCsv.forEach(row => {
res.write(row.join(','))
res.write('\n')
})
res.end()
})

app.listen(3000)
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
61 changes: 47 additions & 14 deletions 03-react/src/components/pages/Exercise/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import './assets/styles.css'
import { useState } from 'react'
import React, { useState, useEffect } from 'react'

export default function Exercise01 () {
const movies = [
Expand Down Expand Up @@ -40,23 +40,54 @@ export default function Exercise01 () {
}
]

const [cart, setCart] = useState([
{
id: 1,
name: 'Star Wars',
price: 20,
quantity: 2
const [cart, setCart] = useState([])
const [discount, setDiscount] = useState([])

const addCart = (item) => {
const existId = cart.find(x => x.id === item.id)
if (!existId) {
setCart(cart => ([...cart, {
...item,
quantity: 1
}]))
}
])
}

const handleQuantity = (id, isDecrement = false) => {
const item = cart.find(x => x.id === id)
item.quantity = item.quantity + (isDecrement ? -1 : 1)

const getTotal = () => 0 // TODO: Implement this
if (item.quantity === 0) {
const newCart = cart.filter(x => x.id !== id)
setCart(newCart)
} else {
setCart([...cart])
}
}

const getTotal = () => cart.reduce((acc, x) => acc + (x.price * x.quantity), 0)

const getDiscount = () => {
let currentDiscount = 0
for (const item of discountRules) {
const isValid = cart.reduce((acc, x) => acc && item.m.includes(x.id), true) && item.m.length === cart.length
if (isValid) {
currentDiscount = item.discount
break
}
}
setDiscount(getTotal() * currentDiscount)
}

useEffect(() => {
getDiscount()
}, [cart])
return (
<section className="exercise01">
<div className="movies__list">
<ul>
{movies.map(o => (
<li className="movies__list-card">
<li className="movies__list-card" key={o.id}>
<ul>
<li>
ID: {o.id}
Expand All @@ -68,7 +99,7 @@ export default function Exercise01 () {
Price: ${o.price}
</li>
</ul>
<button onClick={() => console.log('Add to cart', o)}>
<button onClick={() => addCart(o)}>
Add to cart
</button>
</li>
Expand All @@ -78,7 +109,7 @@ export default function Exercise01 () {
<div className="movies__cart">
<ul>
{cart.map(x => (
<li className="movies__cart-card">
<li className="movies__cart-card" key={x.id}>
<ul>
<li>
ID: {x.id}
Expand All @@ -91,21 +122,23 @@ export default function Exercise01 () {
</li>
</ul>
<div className="movies__cart-card-quantity">
<button onClick={() => console.log('Decrement quantity', x)}>
<button onClick={() => handleQuantity(x.id, true)}>
-
</button>
<span>
{x.quantity}
</span>
<button onClick={() => console.log('Increment quantity', x)}>
<button onClick={() => handleQuantity(x.id)}>
+
</button>
</div>
</li>
))}
</ul>
<div className="movies__cart-total">
<p>Discount: ${discount}</p>
<p>Total: ${getTotal()}</p>
<p>Total with discount: ${getTotal() - discount}</p>
</div>
</div>
</section>
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"scripts": {
"1": "nodemon 01-basic-cs/",
"2": "nodemon 02-nodejs/",
"3": "cd 03-react; npm start",
"3": "cd 03-react && npm start",
"lint": "eslint .",
"postinstall": "cd 03-react && npm i"
},
Expand Down