0

I have this code where First Name - Last Name from a textarea will be split and swap places - output will be Last Name - First letter of First name and separated by a "/". However, I have ran out of idea on how to remove the extra "/" at the end of the output.

Input:

John Doe

Jane Doe

Johnny Doe

Desired output: Doe, J / Doe, J / Doe, J

But getting : Doe, J / Doe, J / Doe, J / (with this extra /)

How do I omit this if it is the last?

splitName.onclick = function() {
  document.getElementById("result").innerHTML = '';
  var value = document.getElementById("fullName").value;
  value.split('\n').forEach(fullname => {

    var spaceIndex = fullname.indexOf(" ");
    var firstname;
    var lastname;
        var tournament = "Round # Sixshooter (Tournament Name YYYY):";
    if (spaceIndex == -1) {
      lastname = fullname;
      lastname = "";
    } else {
      firstname = fullname.substring(0, spaceIndex);
      lastname = fullname.substr(spaceIndex + 1);       
    }
       document.getElementById("result").innerHTML += [lastname, firstname[0]].filter(Boolean).join(", ") + " / ";
     
  });
};

<div>
  <textarea cols="50" rows="5" id="fullName" class= ""></textarea>
</div>

<button id="splitName">Click me</button>
<div>
  <br>
</div>
<div class= "border" id="result"></div>

Ced
  • 29
  • 7
  • 1
    Just trim off the last `/`? – Liam Oct 13 '21 at 15:31
  • Does this answer your question? [JavaScript chop/slice/trim off last character in string](https://stackoverflow.com/questions/952924/javascript-chop-slice-trim-off-last-character-in-string) – Liam Oct 13 '21 at 15:32

2 Answers2

3

You can create an array of names and then use Array.join('/')

Example of concatenation with Array.join:

let names = ['Doe, J', 'Doe, J', 'Doe, J'];
document.getElementById("result").innerHTML = names.join(' / ');

will result in

'Doe, J / Doe, J / Doe, J'
Emilio B
  • 56
  • 5
  • If you have an array of names that you add to a textarea doesn't that defeat the purpose of having a text area that users can type in? – Andy Oct 13 '21 at 15:53
  • Obviously you have to get the textarea value and get the names form there. You can create an empty array and In the forEach() you just push the names you find. – Emilio B Oct 13 '21 at 15:56
  • But why do that if you already have the names in an array? – Andy Oct 13 '21 at 15:57
  • 1
    Mine was just an example to show how the Array.join() works without refactoring all the original code. The code you provided with your answer is perfect! – Emilio B Oct 13 '21 at 16:04
  • Thanks for your inputs guys. Novice mistake, I thought slice was not working but it turned out that I'm only omitting the white space at the end. I was using `str.slice(0, -1);` when it should be `str.slice(0, -3);` – Ced Oct 13 '21 at 20:47
2

Using a regular expression might be a little easier. match on the first and last names, and then map over each element in the array the match finds, and then join the array up.

const ta = document.querySelector('textarea');
const regex = /([A-Za-z]+) ([A-Za-z]+)/g;
const arr = ta.value.match(regex);

const mapped = arr.map(el => {
  const [first, last] = el.split(' ');
  return `${last} ${first[0]}`;
});

console.log(mapped.join(' / '));
<textarea>
John Doe

Jane Doe

Billy Joel

Johnny Doe
</textarea>
Andy
  • 61,948
  • 13
  • 68
  • 95