From b805f9e3b92f7df22343a6c3b11908abeff99d0f Mon Sep 17 00:00:00 2001 From: Michael Stanton Date: Sun, 7 Dec 2014 12:02:56 +0100 Subject: [PATCH] Turning climb grades into something better. --- climbgrades/module.js | 149 ++++++++++++++++++++++++++++-------------- 1 file changed, 101 insertions(+), 48 deletions(-) diff --git a/climbgrades/module.js b/climbgrades/module.js index 659ea95..ce9a90f 100644 --- a/climbgrades/module.js +++ b/climbgrades/module.js @@ -1,68 +1,121 @@ +var ClimbGrades = (function() { + + // index UIAA YDS French + var chart = [ + [0, "3", "5.2", "1"], + [1, "3/3+", "5.2", "1"], + [2, "3+", "5.3", "2"], + [3, "3+/4-", "5.3", "2"], + [4, "4-", "5.4", "3"], + [5, "4-/4", "5.4", "3"], + [6, "4", "5.4", "3"], + [7, "4/4+", "5.5", "4a"], + [8, "4+", "5.5", "4a"], + [9, "4+/5-", "5.5", "4a"], + [10, "5-", "5.6", "4b"], + [11, "5-/5", "5.6", "4b"], + [12, "5", "5.6", "4b"], + [13, "5/5+", "5.7", "4c"], + [14, "5+", "5.7", "4c"], + [15, "5+/6-", "5.8", "5a"], + [16, "6-", "5.8", "5a"], + [17, "6-/6", "5.9", "5b"], + [18, "6", "5.9", "5b"], + [19, "6/6+", "5.10a", "5c"], + [20, "6+", "5.10a", "5c"], + [21, "6+/7-", "5.10b", "6a"], + [22, "7-", "5.10c", "6a+"], + [23, "7-/7", "5.10c", "6a+"], + [24, "7", "5.10d", "6b"], + [25, "7/7+", "5.10d", "6b"], + [26, "7+", "5.11a", "6b+"], + [27, "7+/8-", "5.11b", "6c"], + [28, "8-", "5.11c", "6c+"], + [29, "8-/8", "5.11c", "6c+"], + [30, "8", "5.11d", "7a"], + [31, "8/8+", "5.12a", "7a+"], + [32, "8+", "5.12a", "7a+"], + [33, "8+/9-", "5.12b", "7b"], + [34, "9-", "5.12c", "7b+"], + [35, "9-/9", "5.12c", "7b+"], + [36, "9", "5.12d", "7c"], + [37, "9/9+", "5.13a", "7c+"], + [38, "9+", "5.13a", "7c+"], + [39, "9+/10-", "5.13b", "8a"], + [40, "10-", "5.13c", "8a+"], + [41, "10-/10", "5.13c", "8a+"], + [42, "10", "5.13d", "8b"], + [43, "10/10+", "5.14a", "8b+"] + ]; + + + var GRADE = { + UIAA: { value: 1, name: "UIAA" }, + YDS: { value: 2, name: "YDS" }, + FRENCH: { value: 3, name: "French" } + }; + + + // Use like FromIndex(3, GRADE.UIAA); + // returns string "3+/4-" + function FromIndex(index, grade) { + if (isNaN(parseInt(index)) || index < 0 || index >= chart.length) { + throw new TypeError("Invalid index parameter."); + } -var uiaa_pattern = /^([3-9])([\+\-])?(?:(?:(\/)[3-9](?:[\+\-])?)?)?$/; + if (!grade.hasOwnProperty("name") || + !grade.hasOwnProperty("value")) { + throw new TypeError("invalid grade parameter."); + } -// Grades -// 3+ -// 5+/6- -function GetSportGradeFromUIAA(rating) { - return 3.0 * rating - 7.0; -} + if (isNaN(parseInt(grade.value)) || grade.value < 1 || grade.value > 3) { + throw new TypeError("grade.value is invalid."); + } + return chart[index][grade.value]; + } -function GetUIAAFromSportGrade(grade) { - return (grade + 7.0) / 3.0; -} + function ToIndex(string_value, grade) { + if (!grade.hasOwnProperty("value")) { + throw new TypeError("invalid grade parameter."); + } -function ParseSportGradeFromUIAA(uiaa) { - m = uiaa_pattern.exec(uiaa); - sport_grade = -1; - if (m !== null) { - grade = m[1]; - sport_grade = GetSportGradeFromUIAA(grade); - if (m[2] != undefined) { - if (m[2] === '+') { - sport_grade = sport_grade + 1; - } else { - sport_grade = sport_grade - 1; - } + if (isNaN(parseInt(grade.value)) || grade.value < 1 || grade.value > 3) { + throw new TypeError("grade.value is invalid."); } - if (m[3] != undefined) { - sport_grade = sport_grade + 0.5; + var column = grade.value; + for (var i = 0; i < chart.length; i++) { + if (chart[i][column] === string_value) { + return i; + } } + return -1; } - return sport_grade; + var module = { + GRADE: GRADE, + FromIndex: FromIndex, + ToIndex: ToIndex + }; + + return module; +}()); + +function ParseSportGradeFromUIAA(uiaa) { + var trimmed_uiaa = uiaa.trim(); + var index = ClimbGrades.ToIndex(trimmed_uiaa, ClimbGrades.GRADE.UIAA); + return index; } function UIAAFromSportGrade(grade) { - rating = GetUIAAFromSportGrade(Math.round(grade)); - rating_decimal = Math.floor(Math.round(rating,1)); - slash_symbol = ""; - if (((rating_decimal + 1) - rating) <= 0.5) { - slash_symbol = "/"; - } - follow_symbol = ""; - delta_from_previous = GetSportGradeFromUIAA(rating_decimal - 1); - if (delta_from_previous === 2) { - follow_symbol = "-"; - } else if (delta_from_previous === 4) { - follow_symbol = "+"; - } - // recurse if needed - follow_on = ""; - if (slash_symbol === "/") { - follow_on = UIAAFromSportGrade(Math.ceil(Math.round(grade)) + 1); - } - - str_rating = rating_decimal.toString() + follow_symbol + slash_symbol + follow_on; - return str_rating; + var uiaa_string = ClimbGrades.FromIndex(grade, ClimbGrades.GRADE.UIAA); + return uiaa_string; } - function print(str) { console.log(str); } @@ -90,7 +143,7 @@ function Main() { } } - average = accum / climb_count; + average = Math.round(accum / climb_count); str_average_rating = UIAAFromSportGrade(average); print("UIAA average rating -> " + str_average_rating); print("Average sport grade -> " + average);