Skip to content

Instantly share code, notes, and snippets.

@gil00pita
Created September 16, 2019 12:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gil00pita/ed6e291d35776c0ec08dcee0e7d0e3b5 to your computer and use it in GitHub Desktop.
Save gil00pita/ed6e291d35776c0ec08dcee0e7d0e3b5 to your computer and use it in GitHub Desktop.
<template>
<div>
<h1 class="title">Student Averages</h1>
<table>
<thead>
<tr>
<th>Name</th>
<th>Score</th>
<th>Grade</th>
<th>Teachers</th>
</tr>
</thead>
<tbody>
<tr v-for="record in sorted" :key="record.student">
<td>{{ record.student }}</td>
<td>{{ record.average }}</td>
<td>{{ record.grade }}</td>
<td>{{ record.teachers }}</td>
</tr>
</tbody>
</table>
<details>
<summary>Get JSON</summary>
<pre>{{ json }}</pre>
</details>
</div>
</template>
<script>
import { average } from '~/utils/index'
const grades = {
a: Symbol('A'),
b: Symbol('B'),
c: Symbol('C'),
d: Symbol('D'),
f: Symbol('F')
}
const recordMap = new Map()
function getGrade(average) {
if (average >= 95) {
return grades.a.description
}
if (average >= 85) {
return grades.b.description
}
return grades.c.description
}
function transformRecord(record) {
if (recordMap.has(record.id)) {
return recordMap.get(record.id)
}
const avg = average(record.scores.flat(2))
const grade = getGrade(avg)
const result = {
student: record.student.trim(),
average: avg,
teachers: record.teachers.split(',').map(teacher => teacher.trimEnd()).join(','),
grade
}
recordMap.set(record.id, result)
return result
}
export default {
props: {
records: {
type: Array,
default: () => []
}
},
computed: {
recordsMap() {
this.records.map(record => transformRecord(record))
return recordMap;
},
sorted() {
const mapped = this.records.map(record => transformRecord(record))
return mapped.sort((a, b) => a.average - b.average)
},
json() {
if (!Object.fromEntries) {
return 'loading...'
}
return JSON.stringify(Object.fromEntries(this.recordsMap), null, 2)
}
},
watch: {
records() {
recordMap.clear()
}
}
}
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment