0
function ArrayAdditionI(arr) {  
  var numbers = arr();
   var arraySum = "";
   for (var i = 0; i < numbers.length; i++) {
        arraySum = arraySum + arr[i];
};
if (numbers.max() <= arraySum) {
  arr = true
}
else if (numbers.max() > arraySum) {
   arr = false;
}

  return arr;            
} 

I need to find the numbers stored in an array called arr and check if they add up to or total the greatest number or whether they do not. If so, return true. If not, return false.

I am not sure I am calling the array correctly in the beginning.

Thanks

1
  • What's up with this line var arraySum = ""; Why is sum a string? Commented Feb 3, 2012 at 23:45

5 Answers 5

3

I actually wrote a library I use just for functions like this.

http://code.google.com/p/pseudosavant/downloads/detail?name=psMathStats.min.js

You would just do this:

var arr = [1,2,3,4,5,300];
if (arr.max() > arr.sum()){
    // Max is greater than sum...
}

One warning though. This library prototypes the Array object which could mess up other scripting that uses for (var i in arr) on an Array, which you shouldn't ever do. I am actually almost done with v2 of the library with a number of new functions and it no longer prototypes the Array object.

You can just grab the .max() and .sum() methods from the code, and use them without the prototyping if you want though.

maxArray = function (arr) {
    return Math.max.apply(Math, arr);
}

sumArray = function (arr) {
    for (var i = 0, length = arr.length, sum = 0; i < length; sum += arr[i++]);
    return sum;
}
Sign up to request clarification or add additional context in comments.

Comments

2

You mean something like this?

function ArrayAdditionI(arr) {  

    for (var i = 0, sum=0; i < arr.length; i++) {
        sum += arr[i];
    }

    return Math.max.apply( Math, arr ) <= sum;
}

Comments

2
function ArrayAdditionI(input) {  
   var arraySum, max;
   arraySum = max = input[0];
   for (var i = 1; i < input.length; i++) {
        arraySum += input[i];
        if(input[i] > max){
           max = input[i];
        }
   };

   return arraySum >= max;          
} 

Comments

1

If the numbers are positive, the answer is guaranteed - the sum is always greater than or equal to the max. If you need to calculate it, ddlshack's code looks good.

Looking at your code, there are a number of issues. First of all, arr() should error out. Arrays aren't functions, and trying to treat them as a function does nothing. Your array is already usable when it is passed in. Additionally, you want to initialize arraySum to 0, not "". The way you are doing it, the values in the array will be coerced into strings and concatenated together, which is not what you are looking for. Finally, arrays don't implement a max() method, but Math does, and functions/methods in javascript can be applied to an array in the manner shown by ddlshack and others.

Comments

1

There are some syntax errors: type missmatch, wrong assign and calls to method that doesn't exists. If I'm understanding what do you want to do, this is the correct code(if changing items order is not a problem):

function ArrayAdditionI(arr) {  
   var ret = false;
   var arraySum = 0;
   for (var i = 0; i < arr.length; i++) {
        arraySum += arr[i];
   }
   if (arr.sort()[arr.length-1] <= arraySum) {
     ret = true
   }
   return ret;
} 

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.