educative.io

Educative

Stuck on Exercise

image

Its like it goes from understandable to “where did that come from??” in one questions…
I have reread all the content but do not see a way in JS to count all numbers between an item & then sum it…

Where did I miss this learning?

Hi @jlc,

This is Fatimah Abdullah from Educative. Thank you for reaching out to us!

I understand that coding exercises can feel overwhelming. However, please be assured that whatever you are being tested on is related to the topic that you have just covered. Do not panic and take some time to think about the problem and break it down.

The for loop is being discussed in this topic. And, you already know that you can create a loop starting from a number i to a number n such that i >= n like so:

for(var i = 0; i <= n; i++){
/* code here */
}

Right? That was simple enough. Now, do you think you can use a simple for loop to help you with the sum?
The exercise states that:

For example, rangeSum(5) would sum the numbers 0, 1, 2, 3, 4, and 5 for a final value of 15.

So, for number = 5, we need 0, 1, 2, 3, 4, and 5. Can we get these numbers using a for loop?
Yes! A simple for loop that is initialized from 0 and then increments till number can be used.

for(var i = 0; i <= number; i++){
/* code here */
}

Now, the only part left is the sum. The exercise stub already gives us a variable to store the sum. Let’s edit that stub to incorporate this for loop.

var rangeSum = function(number) {
  var sum = 0;
  for(var i = 0; i <= number; i++){
     /* code here */
  }
  return sum;
}

In the above-given code, the value of i is different in each iteration of the loop. Also, that is the value that we want to add in the sum. So, shouldn’t it be enough to write: sum = sum + i inside the loop?

var rangeSum = function(number) {
  var sum = 0;
  for(var i = 0; i <= number; i++){
     sum = sum + 1;
  }
  return sum;
}

We had already studied all of these concepts, right?

Note that this is not the final solution to the challenge. This does not handle the case of negative input. However, I am confident that you can solve that on your own now.
(Hint: You have to use an if-else condition and another for-loop.)

I hope my explanation has been of help. Please feel free to reach out to me again if you have any questions regarding this problem or any other.

Best Regards,

Fatimah Abdullah | Developer Advocate

Educative

I tried this…

var rangeSum = function(number) {
  var sum = 0;
  /* write your code here */
  if (number > 0) {
for (var i = 0; i <= number; i++) {
  sum = sum + i;
}
  }
  if (number < 0) {
for (var i = 0; i <= number; i--) {
  sum = sum - i;
}
  }
  return sum;
}

Edit 2
I think I’m not thinking of how the numbers are working right…

var rangeSum = function(number) {
  var sum = 0;
  /* write your code here */
  if (number > 0) {
    for (var i = 0; i <= number; i++) {
      sum = sum + i;
    }
  }
  if (number < 0) {
    for (var i = -42; i <= number; i++) {
      sum = sum + i;
    }
  }
  return sum;
}

Edit 3
Getting closer…

var rangeSum = function(number) {
  var sum = 0;
  /* write your code here */
  if (number > 0) {
    for (var i = 0; i <= number; i++) {
      sum = sum + i;
    }
  }
  if (number < 0) {
    for (var i = 0; i >= number; i--) {
      sum = sum - i;
    }
  }
  return sum;
}

Edit 4
Yeah! I was never good at math… if you subtract a negative its like adding a positive… swapped sum -i to sum + i because i will be a negative… so! thank you :slight_smile:

var rangeSum = function(number) {
  var sum = 0;
  /* write your code here */
  if (number > 0) {
    for (var i = 0; i <= number; i++) {
      sum = sum + i;
    }
  }
  if (number < 0) {
    for (var i = 0; i >= number; i--) {
      sum = sum + i;
    }
  }
  return sum;
}
2 Likes

Hi @FatimahAbdullah , I am starting JS tutorial and as @jlc I stucked at this exercise to. Although you specified in a task to use concepts I have already learned I could not find a solution with the use of a loop. SInce there was no obligation to use a loop I tried different approach. Can you please let me know if my solution is ok too since it works or is there a chance of possible mistake which I did not see?
var rangeSum = function(number) {
if (number>0){
return number*(number+1)/2;
rangeSum(number);
}
else{
return number*(number-1)/-2;
rangeSum(number);
}
}