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

536
Visualizações
Javascript: Get nth character of string (JSChallenger)

I am a total newbie and currently learning Javacript. I encountered this problem on JSChallenger and have been struggling with it. Here's my code:

// Write a function that takes a string (a) and a number (n) as argument
// Return the nth character of 'a'
function myFunction(a, n)
{let string = a;
let index = n;
return string.charAt(index);
}

Can anyone point out my errors? Thanks so much!

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

0

Try this one

function myFunction(a, n){
return a[n - 1];}
about 4 years ago · Juan Pablo Isaza Relatório

0

// there is a shorter way to do this since you want to find what's missing here is what's missing. 
function myFunction(a, n){
    let string = a;
    let index = n-1;
    return string.charAt(index);
}
about 4 years ago · Juan Pablo Isaza Relatório

0

JS string's index starts enumerating from 0 so the n should be decremented by 1

// Write a function that takes a string (a) and a number (n) as argument
// Return the nth character of 'a'
function myFunction(a, n)
{
    let string = a;
    let index = n;
    return string.charAt(index-1);
}

the easiest way to write it would be:

// Write a function that takes a string (a) and a number (n) as argument
// Return the nth character of 'a'

function myFunction(a,n) {
   return a[n - 1];
}

Meaning return from string "a" of myFunction the index "[n-1]" , "n-1" it's a needed operation to get the right index because string index start enumerating from 0.

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