JSFiddle - React, Tailwind, and code Playground

by alexbfree

JavaScript

function get_sum_of_squares(array_of_ints) {
  let total = 0;
  for (let i of array_of_ints) {
    total += (i*i);
  }
  return total;
}

/**
 * Function to calculate quadratic score for a given bin
 *
 * @param alpha Scaling parameter alpha
 * @param beta Scaling parameter beta
 * @param bin_allocations Array of integer bin allocations - each value must be between 0 and 100
 * @param index_of_bin_to_score The index into the array, between 0 and (bin_allocations - 1), for the bin whose
 * score is being requested
 * @returns {number} The quadratic score for this bin
 */
function get_quadratic_score(alpha, beta, bin_allocations, index_of_bin_to_score) {
  let sum_of_squares = get_sum_of_squares(bin_allocations);
  console.log(bin_allocations);
  let allocation_for_this_bin = parseInt(bin_allocations[index_of_bin_to_score]);
  console.log(`alloc for ${index_of_bin_to_score} is ${allocation_for_this_bin}`);
  let score = alpha + ( beta * ((2 * allocation_for_this_bin) - sum_of_squares));
  return score;
}

console.log(get_quadratic_score(50,50,[0,0,0],0));
console.log(get_quadratic_score(50,50,[0,0,0],1));
console.log(get_quadratic_score(50,50,[0,0,0],2));