Var i = 1 and var i = 0

Hello, what’s the difference between for (var i = 0; i < something.length; i++) and for (var i = 1; i < something.length; i++) ? Thank you

Edit 6/4/2020:
I know about indexes, but this code here is using both var i = 0 and var i = 1 . Here it is:

function getLongestWordOfMixedElements(arr) {
  if (arr.length === 0) return ''

var words = []
  for (var i = 0; i < arr.length; i++) {
    if (typeof arr[i] === 'string') {
     words.push(arr[i])
    }
  }

  if (words.length === 0) return ''

var longestWord = words[0]
  for (var i = 1; i < words.length; i++) {
    if (words.length > longestWord.length) {
      longestWord = words[i]
    }
  }
  return longestWord
}

var output = getLongestWordOfMixedElements([3, 'word', 5, 'up', 3, 1]);
console.log(output); // --> 'word'

I don’t understand why anybody would use var i = 1 . Why would you start to iterate at the second value?

Let’s assume something is the array ['a', 'b'].

Remember that arrays are indexed starting from zero, so to access the elements 'a' and 'b' you can do:

  • something[0] gives you 'a'
  • something[1] gives you 'b'

In your loop, if i starts by being 0, you get:

something[0] and something[1] ('a' and 'b')

If i starts at 1, you skip the something[0] entirely, and only get 'b'.

1 Like

One starts at 0 and one starts at 1 : )

In the first case, you will use values of i from 0 to something.length - 1. This is a total of something.length values.

In the second case, you will use values of i from 1 to something.length - 1. This is a total of something.length - 1 values.

You almost certainly want the first one, especially if you are indexing into an array, as the first entry is at something[0].

1 Like

I know about indexes, but this code here is using both var i = 0 and var i = 1. Here it is:

function getLongestWordOfMixedElements(arr) {
  if (arr.length === 0) return ''

var words = []
  for (var i = 0; i < arr.length; i++) { // <-- here
    if (typeof arr[i] === 'string') {
     words.push(arr[i])
    }
  }

  if (words.length === 0) return ''

var longestWord = words[0]
  for (var i = 1; i < words.length; i++) { // <-- and here
    if (words.length > longestWord.length) {
      longestWord = words[i]
    }
  }
  return longestWord
}

var output = getLongestWordOfMixedElements([3, 'word', 5, 'up', 3, 1]);
console.log(output);  // --> 'word'

I don’t understand why anybody would use var i = 1. Why would you start to iterate at the second value?

Ah, different question that your initial question.

In this case the two for loops are doing two different things.

In the first for loop, the array words is being populated with all elemnts of arr, so you need to loop over every element in arr.

In the second for loop, we have assumed that words[0] is the longest word and are checking every other entry in words to see if any of them are longer. In this case, it makes no sense to check if words[0] is longer than words[0], so i = 0 is skipped and the loop starts at i = 1.

1 Like

Ah! Got it, thank you!

1 Like

I’m happy to have helped. Good question.

1 Like