0

I have what I think surely is a really simple question for most of you. But I have some trouble to get my head around this for loop. What does the -1 in argument.length -1 stand for? Is it the last item? And the i-- that is for decrease by 1?

var plus = function() {
var sum = 0;
 for (var i = arguments.length - 1; i >= 0; i--) {
     sum += arguments[i];
 }
  return sum;
 }

console.log(plus(2,2,3,657,5643,4465,2,45,6));
hbrovell
  • 409
  • 4
  • 13
  • 1
    When you have an array of length 5, the last item is item number 4. That's where the `length - 1` comes from. – Niet the Dark Absol Aug 20 '18 at 13:37
  • 1
    It's the index of the last item. Array#length is one-based, and the index is zero-based – Luca Kiebel Aug 20 '18 at 13:37
  • 1
    I suggest you read the [`for`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for) documentation – Zenoo Aug 20 '18 at 13:37
  • Array starts at index zero so length is one more than the last index. – epascarello Aug 20 '18 at 13:40
  • 1
    You’re using the console already; your browser has a `debugger` functionality. You can use it to observe how the code runs, observe values, etc. You could also type `console.log(arguments.length)` and `console.log(arguments.length - 1)`. As for [`i--`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Arithmetic_Operators#Decrement), see [What does this symbol mean in JavaScript?](https://stackoverflow.com/q/9549780/4642212) and the documentation on MDN about [expressions and operators](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators). – Sebastian Simon Aug 20 '18 at 13:41

3 Answers3

1

The - 1 means to subtract 1 from arguments.length. i-- means to decrease i by 1.

jh314
  • 24,533
  • 14
  • 58
  • 79
1

You need to know two things here:

  1. arguments is a object type so it has key-value pair of values you passed as a argument into a function. Furthermore, the arguments object is not an Array. It is similar to an Array, but does not have any Array properties except length.
  2. The key of arguments always starts with 0 and ends with one value less than the length of arguments. See the example below the key ends at 8 so you do arguments.length - 1 so that you get 8 instead of 9.

And since you are looping considering the last value first in arguments you do --i.

var plus = function() {
  console.log(arguments);
  console.log(typeof arguments);
  var sum = 0;
  for (var i = arguments.length - 1; i >= 0; i--) {
    sum += arguments[i];
  }
  return sum;
}

console.log(plus(2, 2, 3, 657, 5643, 4465, 2, 45, 6));

Alternatively, you can also do i++ as,

var plus = function() {
  var sum = 0;
  for (var i = 0; i <arguments.length; i++) {
    sum += arguments[i];
  }
  return sum;
}

console.log(plus(2, 2, 3, 657, 5643, 4465, 2, 45, 6));
Ankit Agarwal
  • 28,439
  • 5
  • 29
  • 55
1

When you call arguments.length It will return you the number of elements with the last one accessed with arguments[arguments.length-1] because counting starts with 0. (the First element is accessed like this arguments[0]).

enter image description here

Here is good documentation for Java but it is the same for JavaScript: https://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html

And yes i-- decreases i for 1. It is different i-- and --i.

Using ++/-- After the Operand

When you use the increment/decrement operator after the operand, the value will be returned before the operand is increased/decreased.

Check out this example:

// Increment
let a = 1;
console.log(a++);    // 1
console.log(a);      // 2
// Decrement
let b = 1;
console.log(b--);    // 1
console.log(b);      // 0

When we first log out the value of a, or b, neither has changed. That’s because the original value of the operand is being returned prior to the operand being changed. The next time the operator is used, we get the result of the +1, or -1.

Using ++/-- Before the Operand

If you’d rather make the variable increment/decrement before returning, you simply have to use the increment/decrement operator before the operand:

// Increment
let a = 1;
console.log(++a);    // 2
console.log(a);      // 2
// Decrement
let b = 1;
console.log(--b);    // 0
console.log(b);      // 0

As you can see in the above example, but using ++ or -- prior to our variable, the operation executes and adds/subtracts 1 prior to returning. This allows us to instantly log out and see the resulting value.

Comp
  • 125
  • 2
  • 11