Understanding the indexOf

8

Why when using the indexOf method should the condition to find the words beginning with "A" be equal to 0?

let teachers =["Alexys",
                "Jon",
                "Daniel",
                "Francisco",
                "Rafa",
                "José",
                "Alvaro"];

for (i = 0; i < teachers.length; i++) {
    if (teachers[i].toUpperCase().indexOf("A") == 0) {
        console.log(teachers[i]);
    } 
}
    
asked by José Ignacio 12.02.2018 в 21:34
source

4 answers

12

The indexOf method returns the position in the chain of the first occurrence of the value passed as a parameter (in this case "A" ).

If there is no occurrence, return a value of -1

0 would indicate that there is an occurrence in the first position (first character) of the chain, 1 that exists in the second position (second character), 2 in the third position, etc.

This way if the string starts with "A" it will return a value of 0 and it will not return a different value.

More information: String.prototype.indexOf

    
answered by 12.02.2018 / 21:39
source
5

The indexOf returns the first index containing a letter (in your case A). if it has no value it will return -1

console.log("alexys".indexOf("a"))
console.log("Papaya".indexOf("a"))

console.log("No tengo ->".indexOf("a"))

Javascript has another form and you do not have to do a for, using the method Filter

let teachers =["alexys",
                "Jon",
                "Daniel",
                "Francisco",
                "Rafa",
                "José",
                "Alvaro"];
let soloA = teachers.filter(function(str){ 
                      return str.toUpperCase().startsWith('A')
            })
           
console.log(soloA)
    
answered by 12.02.2018 в 21:43
5

Because indexOf is governed by the first letter in your example that you want "A" as the first letter, other cases would be for example:

 console.log("Asd".indexOf("A")) //devuelve 0 porque la A esta en la posición 0
                                // la cadena Asd la vería como ["A","s","d"]
    
console.log("Asd".indexOf("B")) // devuelve -1 porque "B" no se encuentra 
                                 //en la cadena

console.log("Dsd".indexOf("d")) //devuelve 2 porque la "d" se encuentra 
                                //en la posición 2 de la cadena

This is why if you search for the words that start with a certain letter you should check the position 0 of the string, which is the letter it starts with. p>

  

Note: Note in the third example that the method is case-sensitive.

If you use a second parameter you specify where you start to analyze the string, for example:

console.log('asd'.indexOf("s",0)) // este es el parámetro por defecto
                                   // se retornara 1(posición de "s")

console.log('asd'.indexOf("s",1)) // se comienza en la misma posición de "s"
                                  //   por tanto retornara 1

console.log('asd'.indexOf("s",2)) //en este caso se comenzara en la letra "d"
                                  //por lo que no se analizara "s" y retornara -1

console.log('asd'.indexOf("s",4)) //en el este caso de igual manera retornara -1 dado que estaría buscando en valores fuera de rango de la cadena

console.log("asd".indexOf("",4))  //en este caso al intentar realizar la búsqueda con un indice 
                    // mayor a la cantidad de elementos y como criterio de búsqueda ""
                     // se retornara 3 equivalente a la propiedad .length de la cadena
console.log('asd'.length) //3
    
answered by 12.02.2018 в 21:45
4

Strings are an array of characters whose indexes start at 0, if you want to know if a string starts with a specific letter you should verify its first occurrence, that is, index 0.

    
answered by 12.02.2018 в 21:40