Forem

Cover image for Resolvendo exercícios do JSchallenger - (Javascript Basics #4)
ananopaisdojavascript
ananopaisdojavascript

Posted on

2 2

Resolvendo exercícios do JSchallenger - (Javascript Basics #4)

O que deve ser feito aqui?

  • Escrever uma função que obtenha uma cadeia de caracteres (a) e um número (n) como argumentos
  • Retornar o caractere n de a

Sabemos que números negativos são utilizados para referirem-se aos últimos caracteres de uma string. Para identificar esse caractere no índice n, vamos fazer a seguinte operação: pegar o número total de caracteres e subtraí-lo por -1, dentro de uma notação de colchetes.

function myFunction(a, n) {
   return a[n - 1]
}
Enter fullscreen mode Exit fullscreen mode

Resultados

myFunction('abcd',1) // 'a'
myFunction('zyxbwpl',5) // 'w'
myFunction('gfedcba',3) // 'e'
Enter fullscreen mode Exit fullscreen mode

A quem interessar possa, o link do desafio está aqui:

Get nth character of string

Gostou? Até a próxima anotação! :)

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Immerse yourself in a wealth of knowledge with this piece, supported by the inclusive DEV Community—every developer, no matter where they are in their journey, is invited to contribute to our collective wisdom.

A simple “thank you” goes a long way—express your gratitude below in the comments!

Gathering insights enriches our journey on DEV and fortifies our community ties. Did you find this article valuable? Taking a moment to thank the author can have a significant impact.

Okay