0

This is the javascript I'm trying to run in the terminal via jsc:

calc_real_weights.js:

getElementWithLowestCount(someMatrix)

Array.prototype.max = function() {
  return Math.max.apply(null, this);
};

Array.prototype.min = function() {
  return Math.min.apply(null, this);
};

function getElementWithLowestCount(countAndWeightMatrix){
    aux = []
    for(var i = 0; i < countAndWeightMatrix.length; i++){
        if (countAndWeightMatrix[i][0] < aux.min()){
          var min = countAndWeightMatrix[i];
        }
        aux.push(countAndWeightMatrix[i][0]);
    }
    return min;
}

The error:

Borjas-MacBook-Pro:Algorithm borjagvo$ jsc calc_real_weights.js
Exception: TypeError: undefined is not a function (evaluating 'aux.min()')
getElementWithLowestCount@calc_real_weights.js:37:46
calcRealWights@calc_real_weights.js:46:45
global code@calc_real_weights.js:18:33

It looks like getElementWithLowestCount doesn't see the Array prototype extension. How can I make it to see it?

Thanks.

Community
  • 1
  • 1
borjagvo
  • 1,802
  • 2
  • 20
  • 35

1 Answers1

2

I think that the problems comes from the fact that you are calling getElementWithLowestCount before changing the Array prototype. Try moving your call to getElementWithLowestCount after the two Array.prototipe blocks.

Mir
  • 1,575
  • 1
  • 18
  • 31