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

153
Visualizações
Get array index at which character at n-th index is found if strings in array were concatenated

I have this array [ "one", "two", "three" ].

Now, given an index n, how can I find the index of the array element, in which the character at index n is located? By character at index n I mean the character of all the strings overall, i.e. of "onetwothree". For example: the character at index n = 2 is "e", for n = 3, it’s "t", for n = 4 it’s "w", etc.

The result I’m looking for is the index of the array element where the character at index n was originally found.

Examples for the given array:

Input Output … because the character at index n was
n = 2 0 "e" from "one"
n = 3 1 "t" from "two"
n = 4 1 "w" from "two"
n = 9 2 "e" from "three"
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

You could find the index by taking the wanted count and subtract the length of the item.

const
    findIndex = (array, count) => array.findIndex(s => (count -= s.length) <= 0),
    array = ["one", "two", "three"];

console.log(findIndex(array, 5));

about 4 years ago · Juan Pablo Isaza Relatório

0

You could traverse the array and keep track of sum of item lengths. As soon as you see the sum has exceeded the target index, it means you found the element index.

let getIndex = (data, dest) => {

    let sum = 0;

    for (let i = 0; i < data.length; i++) {
        sum += data[i].length
        if (dest < sum) {
            return i;
        }
        
    }
    
    return -1;

}

console.log(getIndex(["one", "two", "three"], 4));

Assumes dest is zero based index.

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