0

I am working on a coding exercise for freeCodeCamp. The purpose of the exercise is to take an array as input that contains 2 numbers. It should return the sum of those two numbers and all numbers between them.

I tried writing a simple for loop using Math.min(), and Math.max() as parameters, but somehow my funtion always returns 0. The basic logic is for the for loop to start at the lowest number in the array, then increment by 1 until it gets to the biggest number in the array, and always add the number to the output variable.

function sumAll(arr) {
  var outcome = 0;
  for(var x = Math.min(arr); x<Math.max(arr); x++) {
    outcome += x;
  }
  return outcome;
}

sumAll([1, 4]);

Any help is appreciated.

2

8 Answers 8

1

Math.min and Math.max don't accept a list as argument. If you want to apply a list to them, use Math.min.apply(Math, arr).

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

Comments

1

Math.min and Math.max does not take an array as argument, but a list of numbers as separate arguments.

To make those methods work on array, use this trick:

Math.min.apply(Math, [1,2,3]) // --> 1

The same goes for Math.max

Comments

1

You are using Math.max and Math.min wrong. To use them with arrays, you can use the spread operator. (Note: the spread operator is new in ECMAScript 6, so it isn't supported in older browsers.) See below:

function sumAll(arr) {
  var outcome = 0;
  for(var x = Math.min(...arr); x<Math.max(...arr); x++) {
    outcome += x;
  }
  return outcome;
}

sumAll([1, 4]);

See the MDN for more details:

Comments

0

The problem is that Math.min () and Math.max () don't take arrays as a parameter. Is this an acceptable alternative?

function sumAll(min, max) {
  var outcome = 0;
  for(var x = min; x<max; x++) {
    outcome += x;
  }
  return outcome;
}

sumAll(1,4);

Some others have provided ways to get the lowest/highest numbers out of an array.

1 Comment

The exercise specifically says that it has to take an array as input.
0

The error is in Math.min(arr). Math.min(arr) evaluated to NaN. If you need to get minimal number value from arr use Math.min.apply(Math, arr) instead.

Good Luck!

Comments

0

try this

 function sumAll(arr) {
  console.log(arr);
  var outcome = 0;
  for(var x = Math.min(...ar1); x<Math.max(...ar1); x++) {
    outcome += x;
  }
  return outcome;
}

Math.min(...ar1) the three dots inside the paranthesis is called spread operator

The spread operator allows an expression to be expanded in places where multiple arguments (for function calls) or multiple elements (for array literals) are expected.

page link

Comments

0
function sum(arr) {
    var min = arr[0] < arr[1] ? arr[0] : arr[1],
    max = arr[0] > arr[1] ? arr[0] : arr[1],
    result = 0;

    for (var i = min; i <= max; i++) {
      result += i;
    }

    return result;
 }

Comments

0

Actually your code will never enter into the for loop because in the for loop initialisation part you are assigning x with the min value of array and then once agin you are comparing x with min of arrayx<Math.max(arr) so the condition fails. then the outcome is always 0.

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.