Generalized topn variable.

This commit is contained in:
2022-12-02 14:42:15 +01:00
parent 7419970f41
commit f5cac113d2
2 changed files with 8 additions and 5 deletions

View File

@@ -1,6 +1,7 @@
const ASSIGNMENT = 'assignment';
const ANSWER = 'answer';
const NEWLINE_CHARACTER = '\n';
const TOP_N = 3;
/**
* Main function
@@ -15,7 +16,7 @@ window.onload = function() {
*/
function calculateAnswer(event) {
console.info("Calculating answer for input...");
let answer = algorithm(event.target.value);
let answer = algorithm(event.target.value, TOP_N);
document.getElementById(ANSWER).innerText = answer;
}
@@ -23,11 +24,12 @@ function calculateAnswer(event) {
/**
* Calculate the answer to assignment1 with 2 variables, count and highest.
* @param assignment the input from the assignment.
* @param topn the
* @return string the elf with the highest calory count
*/
function algorithm(assignment) {
function algorithm(assignment, topn) {
let currentCalorieScore = 0;
let highestCalorieScores = [0,0,0];
let highestCalorieScores = Array.from({length: topn}, () => (0));
let currentElf = 1;
let lines = assignment.trim().split(NEWLINE_CHARACTER);