Javascript exercise

Hi! I am trying to solve this but stuck, I want to find Odd integers between -25000 and 30,000 through the given code plus the sum of all off integers. Can anyone help?

addOddints();

function addOddints() {
let sum = 0;
for (i = -25000; i <= 30000; i += 2) {
if (i % 2 !== 0)
return
sum += i;
}
console.log(i);

}

Putting that into a codeblock for readability:

addOddints();

function addOddints() {
  let sum = 0;
  for (i = -25000; i <= 30000; i += 2) {
    if (i % 2 !== 0) return sum += i;
  }
  console.log(i);
}

That does not look syntactically valid since you cannot return the result of sum += i.

Here are some things you probably need to change:

  1. Instead of adding 2 to i on each run, you want to add 1 (or else change your starting point to be an odd number). As written you are only iterating through the even values (-25000, -24998, …, 29998, 30000).
  2. Instead of returning sum += i, just drop the return keyword and you will simply add i to sum on each iteration where the value is odd.
  3. Instead of logging i, log sum since you want the sum printed out, not the last value of the iterator.

Putting that all together:

addOddInts();

function addOddInts() {
  let sum = 0;
  for (let i = -25000; i <= 30000; i++) {
    if (i % 2 !== 0) sum += i;
  }
  console.log(sum);
}

Hi, Thank you for reply, I want count down from -25000 and end on 30,000 for all odd numbers including sum of all odd numbers. I am asking logically, why return keyword would not work with sum and last one point when we are declaring sum+=i this means that we declared i=sum, so why i can not be used for sum.

I am not sure what you mean by “we declared i=sum” since you do not have any part of your code that sets i = sum. Perhaps you have a misunderstanding of what sum += i does. The expansion of sum += i is sum = sum + i which means reassign the variable sum to the result of taking the current value of sum and adding the current value of i. There is no reassignment of the variable i anywhere in your code except for the increment operation (i++ in my code and i += 2 in yours).

The statement sum += i does not return a value, so there is no meaning for return sum += 1. If it did return a value, what value do you think it should return? Perhaps you think it should return the result of the modification? Let us try re-writing the code as though return sum += i; were the same as sum += i; return sum;:

addOddInts();

function addOddInts() {
  let sum = 0;
  for (let i = -25000; i <= 30000; i++) {
    if (i % 2 !== 0) {
      sum += i;
      return sum;
    }
  }
  console.log(sum);
}

This still does not do what you want because it will return the sum when we hit the first odd value (and print nothing which seems to have been your goal). We can trace the code to confirm:

  1. we enter the addOddInts and initialize the local variable sum to 0
  2. we begin the for loop and initialize i to -25000; we meet the condition i <= 30000 so we enter the body of the loop
  3. i % 2 returns 0 so i % 2 !== 0 returns False therefore we do not enter the body of the if statement and we return to the start of the loop
  4. we apply the increment statement and i is incremented by 1 so i is now -24999; we still meet the condition i <= 30000 so we enter the body of the loop
  5. i % 2 returns -1 so i % 2 !== 0 return True therefore we enter the body of the if statement
  6. sum += i expands to sum = sum + i which results in sum = 0 + -24999 meaning sum is assigned the value -24999.
  7. now we return sum which exits the function addOddInts and nothing is printed

You see how this does not have the desired behavior?

1 Like

Thank you so much for explaining it details. your insights are quite useful for me and I got deeper understanding of for loop.