How does Length and Out of For in Javascript work?

1

I have a constant question about Length and loop for to measure an array or string.

Doubt 1:

When we use this code:

const numero ="teste";
const medir = numero.length;
console.log(medir)

It returns the value 5. But if we use it as an array:

const numero = ["Saab", "Volvo", "BMW"];
const medir = numero.length;
console.log(medir)

It returns 3. Why?

Doubt 2:

How does it work within For? I have the code:

   const separador = " - ";

function filtro(funcao, numeros2) {

let stringNova = '';

for (let i = 0; i < numeros2.length; i++) {

    stringNova += numeros2[i] + (funcao)
}
return stringNova
}

console.log(filtro(separador, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]));
    for (let i = 0; i < numero.length; i++)

How exactly does this snippet work? People explain to me but they explain to me in a technical way and I'm new and it's not so clear to me.

    
asked by anonymous 31.01.2018 / 00:27

2 answers

3

Doubt # 1:

const numero ="teste";
const medir = numero.length;
console.log(medir)

Your variable number is of type text ( string ) the property length returns the amount of characters.

const numero = ["Saab", "Volvo", "BMW"];
const medir = numero.length;
console.log(medir)

Your variable is a vector ( array ) the property length returns the number of elements.

Doubt # 2

Within the for works the same as the two ways I mentioned above, the value of length will be based on the type of the variable.

The first code, returns the number of elements since you are passing an array as a parameter.

const separador = " - ";

function filtro(funcao, numeros2) {

  let stringNova = '';

  for (let i = 0; i < numeros2.length; i++) {
    stringNova += numeros2[i] + (funcao)
  }
  return stringNova
}

console.log(filtro(separador, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]));

And this last piece of code based on the variable numero is the same as the code at the beginning of your question, returns the number of letters, then runs the loop 5 times.

for (let i = 0; i < numero.length; i++)
    
31.01.2018 / 00:30
1

The for loop is usually used to loop through elements in an array. Imagine a street with houses, starting from House 1, House 2, House 3, and so on. In the same way it is an array, an object that contains organized elements such as a street with houses, except that in the array the address begins with House 0 , House 1 etc., that is, in an array, the first address is always 0 (called index or index ).

So, an array with 3 elements, for example, the maximum index it can have is 2, because it starts from 0, 1, 2 (3 items).

var array = ['Maria','Joao','Jose']; // Total de 3 itens
                ↑       ↑      ↑
                0       1      2     // índices

So in for the variable starts with 0 and goes to the total of items - 1 (3 - 1 = 2), which is the maximum index in the array:

for (let i = 0; i < numeros2.length; i++) {
                          ↑
                enquanto "i" for menor que
              a quantidade de itens na array

Since i equals the number of items in the array, the condition in for ( numeros2.length ) will no longer be satisfied and the loop is terminated .

    
31.01.2018 / 11:45