const ASSIGNMENT = 'assignment'; const ANSWER = 'answer'; const ALGORITHM_CHECKBOX = 'algorithm'; const CHARCODE_START=64; const CHARCODE_UPPER_CORRECTION=6; const ERROR_MESSAGE_INVALID_INPUT_SIZE = "Invalid input size, should be higher than 2 for: "; const ERROR_MESSAGE_INVALID_ITEM_CHARACTER = "Invalid item character found: "; const NEWLINE_CHARACTER = '\n'; /** * Main function */ window.onload = function() { document.getElementById(ASSIGNMENT).addEventListener("input", calculateAnswer); document.getElementById(ALGORITHM_CHECKBOX).addEventListener("click", calculateAnswer); } /** * Listener function for input in assignment field. * @param event the onInput event */ function calculateAnswer(event) { console.info("Calculating answer for input..."); let assignment = document.getElementById(ASSIGNMENT).value; let byCompartiments = document.getElementById(ALGORITHM_CHECKBOX).checked; let answer = algorithm(assignment, byCompartiments); document.getElementById(ANSWER).innerText = answer; } /** * Calculate the answer to assignment. * @param assignment the input from the assignment. * @return string the answer */ function algorithm(assignment, byCompartiments) { let priorityScoreSum = 0; let lines = assignment.trim().split(NEWLINE_CHARACTER); console.info("Linecount:" + lines.length); let increment = (byCompartiments)? 1:3; for(let i=0; i