Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

181
Visualizações
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 Respostas
Responde à pergunta

0

There is an "id" inside "Widget"

about 4 years ago · Juan Pablo Isaza Relatório

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda