Created
January 27, 2017 21:50
-
-
Save cjroth/9296eecd209aec9db6531fdd7c45b9e1 to your computer and use it in GitHub Desktop.
Functions for calculating quality-of-life subscores and calculating a weighted-average of them. Written for Google Sheets.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function QOL(steps, awake, sleep, alcohol, food, lifeSatisfaction) { | |
var scores = [ | |
{ | |
score: QOL_STEPS(steps), | |
weight: 1 | |
}, | |
{ | |
score: QOL_AWAKE(awake), | |
weight: 1 | |
}, | |
{ | |
score: QOL_SLEEP(sleep), | |
weight: 1 | |
}, | |
{ | |
score: QOL_ALCOHOL(alcohol), | |
weight: 1 | |
}, | |
{ | |
score: QOL_LIFE_SATISFACTION(lifeSatisfaction), | |
weight: 1 | |
}, | |
{ | |
score: QOL_FOOD(food), | |
weight: 1 | |
}, | |
{ | |
score: QOL_EATING_WINDOW(food), | |
weight: 1 | |
}, | |
]; | |
var scoreTotal = 0; | |
var weightTotal = 0; | |
scores.forEach(function(score) { | |
if (score.score === null) { | |
return; | |
} | |
scoreTotal += score.score * score.weight; | |
weightTotal += score.weight; | |
}); | |
if (weightTotal === 0) { | |
return null; | |
} | |
return Math.round(scoreTotal / weightTotal * 100); | |
} | |
function QOL_STEPS(steps) { | |
if (!steps) { | |
return null; | |
} | |
if (steps > 15000) { | |
steps = 15000; | |
} | |
return steps / 15000; | |
} | |
function QOL_AWAKE(awake) { | |
if (!awake) { | |
return null; | |
} | |
var parts = awake.split(':'); | |
var minutes = parseInt(parts[0], 10) * 60 + parseInt(parts[1], 10); | |
var goal = 8 * 60; | |
var max = 12 * 60; | |
if (minutes >= max) { | |
return 0; | |
} | |
if (minutes <= goal) { | |
return 1; | |
} | |
return (max - minutes) / (max - goal); | |
} | |
function QOL_SLEEP(sleep) { | |
if (!sleep) { | |
return null; | |
} | |
var parts = sleep.split(':'); | |
var minutes = parseInt(parts[0], 10) * 60 + parseInt(parts[1], 10); | |
var goal = 9 * 60; | |
var min = 6 * 60; | |
if (minutes >= goal) { | |
return 1; | |
} | |
if (minutes <= min) { | |
return 0; | |
} | |
return (minutes - min) / (goal - min); | |
} | |
function QOL_ALCOHOL(alcohol) { | |
if (!alcohol) { | |
return 1; | |
} | |
var drinks = alcohol.split('\n').length | |
var score = 1 - drinks * 0.25; | |
if (score < 0) { | |
return 0; | |
} else { | |
return score; | |
} | |
} | |
function QOL_LIFE_SATISFACTION(lifeSatisfaction) { | |
if (!lifeSatisfaction && lifeSatisfaction !== 0) { | |
return null; | |
} | |
return lifeSatisfaction / 10; | |
} | |
function QOL_FOOD(food) { | |
var fails = (food.match(/\(sg|rm|sy|dy|gl\)/ig) || []).length; | |
var score = 1 - fails * 0.25; | |
if (score < 0) { | |
return 0; | |
} else { | |
return score; | |
} | |
} | |
function QOL_EATING_WINDOW(food) { | |
var lastMealTime = food | |
.split('\n') | |
.map(function(meal) { | |
return PARSED_MINUTES(PARSE_TIME(meal.split(' ')[0])); | |
}) | |
.filter(function(time) { | |
return !isNaN(time); | |
}) | |
.sort(function(a, b) { | |
return a - b; | |
}) | |
.pop(); | |
var goal = 20 * 60; | |
var max = 24 * 60; | |
if (lastMealTime >= max) { | |
return 0; | |
} | |
if (lastMealTime <= goal) { | |
return 1; | |
} | |
return (max - lastMealTime) / (max - goal); | |
} | |
function PARSE_LMH(LMH, reverse) { | |
LMH = LMH.replace(/[^L^M^H]/ig, ''); | |
var map = { L: 0.33, M: 0.66, H: 1 }; | |
if (reverse) { | |
return 1 - map[LMH]; | |
} else { | |
return map[LMH]; | |
} | |
} | |
function PARSE_TIME(time) { | |
var parts = time.split(':'); | |
return { | |
hours: parseInt(parts[0], 10), | |
minutes: parseInt(parts[1], 10) | |
}; | |
} | |
function PARSED_MINUTES(parsedTime) { | |
return parsedTime.hours * 60 + parsedTime.minutes; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment