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.

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.