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

168
Visualizações
Get first letter of the string and full last name

I am struggling to figure out how can I refactor my code to get a first letter and last name but if it is only one word to get the word without the first letter. When is only one word getting the first letter as well? E.G If it is name@abc.com then becomes nname@abc.com if it is johndoe@abc.com is getting right jdoe@abc.com is there a way to fix that? I would really appreciate it if someone can help? Here is my Code:

     var fullName = item.getValue('abc12312').toLowerCase().split(' ');
     var lastName = fullName[fullName.length - 1];
     var firstLetter = fullName[0].charAt(0);
     if(fullName != '') {
     var email = firstLetter + lastName + '@abc12123.com';
     }
about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

hadn't tested the code, but mostly it will work

// get input value from HTML and convert it as an array ;
const fullName = document.getElementById('fullname').toLowerCase().split(' ');

let firstLetterOrWord;

if(fullName.length === 1){
  // only one word
  firstLetterOrWord = fullName[0]
} else {
  // two words  
  // first character of first elemment of array 
  firstLetterOrWord = fullName[0][0]

}
// if not found -> empty string
const lastName = fullName[1] || '';

const email = firstLetterOrWord + lastName + '@abc12123.com';
<input id="fullname" type="text">

about 4 years ago · Juan Pablo Isaza Relatório

0

You need to understand what every function you've mentioned in your code block does. Also, please define/mention what item is in your codeblock because we do not have any knowledge/reference as to what it is.

     // get the full name
     var fullName = item.getValue('abc12312').toLowerCase().split(' ');

     // get the last name
     var lastName = fullName[fullName.length - 1];

     // get the first letter
     var firstLetter = fullName[0][0]

     if (lastName != ''){
         const email = firstLetter + lastName + '@abc12123.com';
     }else{
         const email = fullName[0] + '@abc12123.com';
     }
about 4 years ago · Juan Pablo Isaza Relatório

0

All you need to do is create a function/method that:

  1. accepts that string as an input value
  2. count the number of characters in that supplied string value
  3. if it exceeds 2, than you can use JavaScript indexOf or array index methods to return the first and last.
  4. OR return the first char if it's count is lower than 2.
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