1

I'm curious as to why I get two different results when decrementing the arr.length with arr.length-- and arr.length - 1. I would have thought these would have functioned the same. For example:

const numbers = [10, 20, 40, 30, 50]

function solution(numbers) {
        for (let i = 0; i < numbers.length - 1; i++) {
        if (numbers[i] < numbers[i + 1]) {
          console.log('TRUE: ' + numbers[i]+" is increasing to "+ numbers[i+1])
       } else {
         return console.log('FALSE: ' + numbers[i] +' is decreasing to ' + numbers[i+1])
       }
    }
}

logs out as:

"TRUE: 10 is increasing to 20"
"TRUE: 20 is increasing to 40"
"FALSE: 40 is decreasing to 30"

but when I swap in a decrement:

(let i = 0; i < numbers.length--; i++)

the else returns undefined

"TRUE: 10 is increasing to 20"
"TRUE: 20 is increasing to 40"
"FALSE: undefined is decreasing to undefined"
Hezakai
  • 35
  • 4
  • 3
    They're two different things. `numbers.length--` *changes* the value of `numbers.length`, while `numbers.length - 1` does not. – Pointy Mar 27 '23 at 21:02
  • 1
    For the `length` property, decreasing it also has the effect of removing items from the end of the array. Is that what you're aiming to do? It doesn't look like it. – General Grievance Mar 27 '23 at 21:07
  • `something--` is shorthand for `something = something - 1`, so it updates the variable. – Barmar Mar 27 '23 at 21:08
  • I don't understand why they gave -1 to the question when it is a valid question. ‍♂️ – Brian Mar 27 '23 at 21:20
  • 1
    @Brian well it is a valid question, but basic JavaScript syntax is extremely well-documented on the Internet, and a quick search would have saved the OP the time of typing in the question. – Pointy Mar 27 '23 at 23:20

1 Answers1

1

Try this:

console.log(numbers)

You'll see that every loop, the numbers array loses the last value. In JavaScript, array lengths are mutable. You can essentially delete every element in the array by doing array.length = 0.

The -- is the decrement operator, which not only subtracts 1 from the value of numbers.length, but also sets the value of numbers.length to numbers.length - 1.

If you want to control the for-loop correctly, you will always use i < numbers.length - 1, otherwise each loop, you are actually deleting the last element of the array.

Viraj Shah
  • 754
  • 5
  • 19