const ASSIGNMENT = 'assignment'; const ANSWER = 'answer'; const ALGORITHM_CHECKBOX = 'algorithm'; const MARKER_DISTINCT_CHARACTERS = 4; const MESSAGE_DISTINCT_CHARACTERS = 14; 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 distinctCharacters = (document.getElementById(ALGORITHM_CHECKBOX).checked)? MESSAGE_DISTINCT_CHARACTERS: MARKER_DISTINCT_CHARACTERS; let answer = algorithm(assignment, distinctCharacters); document.getElementById(ANSWER).innerText = answer; } /** * Calculate the answer to assignment. * @param assignment the input from the assignment. * @return string the answer */ function algorithm(assignment, distinctCharacters) { let signal = assignment.trim(); let marker = assignment.substring(0,distinctCharacters); for(let i=distinctCharacters; i