Sum of all numbers

Tell us what’s happening:

can someone help me see where I’m going wrong here? I think my logic is correct I just think I may have a minor bug within my implementation.

Thank you!

Your code so far


function sumAll(arr) {

//set sum = 0

let sum = 0

// find min and max

sum += Math.max(arr);
sum += Math.min(arr)

console.log(sum)

//set var = math.min

let middle = (Math.min(arr) + 1)

// while var < math.max
    // add var to sum

while (middle < Math.max(arr)) {
  sum += middle
}

return sum
}

console.log(sumAll([1, 4]));

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.141 Safari/537.36.

Challenge: Sum All Numbers in a Range

Link to the challenge:

Your variable middle never changes, so this loop will never end.

ahh ok, i just added in middle++. but it still deons’t seem to be returning a number

What is your current full code?

function sumAll(arr) {

  //set sum = 0

  let sum = 0

  // find min and max

  sum += Math.max(arr);
  sum += Math.min(arr)

  console.log(sum)

  //set var = math.min

  let middle = (Math.min(arr) + 1)

  // while var < math.max
      // add var to sum

  while (middle < Math.max(arr)) {
    sum += middle
    middle++
  }

  return sum
}

try to look at your code with a tool like this: http://pythontutor.com/javascript.html

you need to add a function call to see the execution of the function

1 Like

I’m not quite sure how to use that tool. I have the function being logged but still not returning a number. can anyone help?

function sumAll(arr) {

  //set sum = 0

  let sum = 0

  // find min and max

  sum += Math.max(arr);
  sum += Math.min(arr);

  console.log(sum)

  //set var = math.min

  let middle = (Math.min(arr) + 1);

  // while var < math.max
      // add var to sum

  while (middle < Math.max(arr)) {
    sum += middle
    middle++
  }

  return sum
}

console.log(sumAll([1, 4]));

The console is showing NaN. So the question is… why?


You have three lines with sum preceding this.

let sum = 0

This line looks benign.

sum += Math.max(arr);
sum += Math.min(arr);

So the issue must be here. But why?


Well, let’s look up Math.max() and Math.min().


Hmm…

The Math.max() function returns the largest of the zero or more numbers given as input parameters, or NaN if any parameter isn’t a number and can’t be converted into one.

NaN means that the input was not a number and can’t be converted to one…


Let’s look at the provided example in the documentation…

console.log(Math.max(1, 3, 2));
// expected output: 3

console.log(Math.max(-1, -3, -2));
// expected output: -1

const array1 = [1, 3, 2];

console.log(Math.max(...array1));
// expected output: 3

Do you see the issue going on?

thank you very much. implementing the rest parameter worked. I think I just need to understand when to implement “…” better

... is called the spread operator/syntax


(I really love how good MDN is)

So you use use the spread syntax mainly when you have an array and you want to break it into a comma separated list of values, such as when passing them into a function or copying them into a new array.

It also comes in handy for destructuring in some cases.

1 Like