Skip to content

Instantly share code, notes, and snippets.

@lieuwex
Last active November 2, 2016 14:45
Show Gist options
  • Save lieuwex/14ce1da07030461a8159 to your computer and use it in GitHub Desktop.
Save lieuwex/14ce1da07030461a8159 to your computer and use it in GitHub Desktop.
Grade calculator connected to Magister
{ Magister } = require 'magister.js'
require! 'kbd'
{ group-by, obj-to-pairs, each, map, reject, is-it-NaN } = require 'prelude-ls'
WANTED = 5.5
ask = (str) ->
process.stdout.write "#str: "
kbd.get-line-sync!
handle-err = (e) !->
console.log "error: #e"
process.exit 1
numberize = (s) -> parse-float s.replace(',', '.'), 10
[schoolname, username, password] = ['Schoolname', 'Username', 'Password'] |> map ask
magister = new Magister do
schoolname
username
password
e <-! magister.ready
handle-err e if e?
e, r <-! @current-course
handle-err e if e?
e, r <-! r.grades do
fillPersons: no
fillGrade: yes
handle-err e if e?
r
|> group-by (.class!abbreviation)
|> obj-to-pairs
|> each ([name, grades]) !->
console.log "#name:"
input = ask 'Weight of next grade'
if input.length is 0
return
weight = input |> numberize
old-grade-weight-sum = 0
old-weight-sum = 0
grades
|> reject (g) -> g.type!type! is 2
|> map (g) ->
grade: (
if g.grade!? then g.grade! |> numberize
else NaN
)
weight: g.weight!
|> reject (g) -> is-it-NaN g.grade
|> each (g) !->
old-grade-weight-sum += g.weight * g.grade
old-weight-sum += g.weight
calculate-grade = (wanted) ->
wanted = 10 if wanted > 10
needed = (wanted * (old-weight-sum + weight) - old-grade-weight-sum) / weight
Math.round(10 * needed) / 10
cur-avg = old-grade-weight-sum / old-weight-sum
cur-rounded-avg = Math.floor(cur-avg + 0.5) - 0.5
next-avg = Math.ceil(cur-avg - 0.4) + 0.5
console.log "Needed grade for #cur-rounded-avg (stay same): #{calculate-grade cur-rounded-avg}"
console.log "Needed grade for #WANTED: #{calculate-grade WANTED}"
console.log "Needed grade for #next-avg (go one up): #{calculate-grade next-avg}"
console.log "---"
@tomsmeding
Copy link

@lieuwex you might want to rename this file for it to be a little bit more meaningful than "index"... ;-)

@lieuwex
Copy link
Author

lieuwex commented Jun 25, 2015

@tomsmeding ¯_(ツ)_/¯

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment