Generalized topn variable.
This commit is contained in:
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user