hbrovell
hbrovell

Reputation: 547

A simple for loop questions about -1 and i--

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));

Upvotes: 0

Views: 400

Answers (3)

Comp
Comp

Reputation: 134

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.

Upvotes: 1

Ankit Agarwal
Ankit Agarwal

Reputation: 30739

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));

Upvotes: 2

jh314
jh314

Reputation: 27802

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

Upvotes: 1

Related Questions