Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

163
Views
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 answers
Answer question

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 Report

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!