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

151
Visualizações
How to find the middle letter among the specified letters?

I've created a function that accepts 2 parameters to find the middle value between those parameters in an Alphabet variable.

Example:

the middle part between Q and U is S,
the middle part between R and U is ST,
the middle part between T and Z is W,

What I'm confused about is how do I take the value one by one starting at index 1 in the Alphabet variable?

function letterMiddleValue(a, b) {
    let alpha1 = Alphabet.indexOf(a);
    let alpha2 = Alphabet.indexOf(b);
    let center = (alpha1 + alpha2) / 2;
    let letterLength;

    if (center % 2 == 1) {
        letterLength = 1;
    } else {
        letterLength = 2;
    }
    return Alphabet.substring(center, center + letterLength);
}

var Alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

console.log(letterMiddleValue("Q", "U"));
console.log(letterMiddleValue("R", "U"));
console.log(letterMiddleValue("T", "Z"));

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

Center for r & u is 18.5. 18.5 % 2 is 0.5 so you need to check for 0.5 condition:

function letterMiddleValue(a, b) {
    let alpha1 = Alphabet.indexOf(a);
    let alpha2 = Alphabet.indexOf(b);
    let center = (alpha1 + alpha2) / 2;
    let letterLength;

    if (center % 2 == 0.5) {
        letterLength = 2;
    } else {
        letterLength = 1;
    }

    return Alphabet.substring(center, center + letterLength);
}

var Alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

console.log(letterMiddleValue("Q", "U"));
console.log(letterMiddleValue("R", "U"));
console.log(letterMiddleValue("T", "Z"));

That's because how you calculate center. (7 + 5) / 2 = 6.5 (6 + 10) / 2 = 8 and modulo gives back what's not dividable by 2

about 4 years ago · Juan Pablo Isaza Relatório

0

I think there is an alternative approach, which is just to calculate the distance between and then decide whether to extract one or two characters.

This also copes with there being nothing between, or the b value coming lower down the alphabet (or not being in the defined Alphabet)

function letterMiddleValue(a, b) {
    const aPos = Alphabet.indexOf(a);
    const bPos = Alphabet.indexOf(b);
    const len = bPos - aPos

    if (len < 2) {
        return '[none]'
    }

    const start = aPos + (len / 2)
    const end = start + 1 + (len % 2)
    return Alphabet.slice(Math.floor(start), end)
}

const Alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

console.log(letterMiddleValue("Q", "U"));   // S
console.log(letterMiddleValue("R", "U"));   // ST
console.log(letterMiddleValue("T", "Z"));   // W
console.log(letterMiddleValue("A", "B"));   // none
console.log(letterMiddleValue("A", "C"));   // B
console.log(letterMiddleValue("A", "D"));   // BC
console.log(letterMiddleValue("A", "A"));   // none
console.log(letterMiddleValue("Z", "A"));   // none
console.log(letterMiddleValue("z", "A"));   // none
console.log(letterMiddleValue("Z", "a"));   // none

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