Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

175
Vistas
why id is found at position 1 in str.indexof() in js

let str = 'Widget with id';

alert( str.indexOf('Widget') ); // 0, because 'Widget' is found at the beginning

alert( str.indexOf('widget') ); // -1, not found, the search is case-sensitive

alert( str.indexOf("id") ); // 1

about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

There is an "id" inside "Widget"

about 4 years ago · Juan Pablo Isaza Denunciar

0

Look closer to your string: "Widget with id" 😄

The first occurrence is found after "W..." which is the index of 1.

W 👉 0

i 👉 1 ✅

d 👉 2

g 👉 3

e 👉 4

t 👉 5

about 4 years ago · Juan Pablo Isaza Denunciar

0

If you would like to find the whole word, you can use a function something like this.

  • Break the string into an array
  • iterate through the array
  • change each word into an array of chars
  • iterate through the chars array and keep a running count of each letter
  • increment your count by one after each word to account for the spaces
  • stop the iteration when the word is found

let str = 'Widget with id';

function findIndexOfWholeWord(str, searchStr){
  const words = str.split(' ')
  let numOfChars = 0
  
  for(let word of words){
   if(word === searchStr) break
    word.split('').forEach(char=>numOfChars++)
    numOfChars++
  }
  return numOfChars
}
console.log(findIndexOfWholeWord(str,'id')) //12

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda