split Javascript separator

1

I have to write a function in which I pass a complete name (string) and return the abbreviated name (the name and first letter of the first surname, followed by a period) - > that is, I type Pepito Perez and Pepito P has to return me.

I am a little confused with the subject of the positions, so that he makes the separation and others:

<!DOCTYPE html>
<html lang="es">
<head>
<meta charset="utf-8" />
<title>funciones</title>
</head> 
<body>
<script type="text/javascript">

var nombre = prompt("Introduce nombre y apellidos");

function nombreCompleto(nombre) {
    var separa = nombre.split(".");
    //var nombre = separa[0];
}

function nombreCompleto();

</script>
</body>
</html>
    
asked by user09b 24.10.2018 в 13:32
source

2 answers

2

The split converts a string into an array by splitting the string from the key we give it.

In your case, the point is something that you need at the end, so the split you have to make the space soft so you can separate the name and surname.

Once we have separated in the 0 position of the array the name and in 1 the last name just need to concatenate it and add the factor that takes the first letter of the last name and adds the point.

All this would be done like this:

<!DOCTYPE html>
<html lang="es">
<head>
<meta charset="utf-8" />
<title>funciones</title>
</head> 
<body>
<script type="text/javascript">

var nombre = prompt("Introduce nombre y apellidos");

var separa = nombre.split(" ");
var nombre = separa[0] + " " + separa[1].charAt(0) + ".";

alert(nombre);



</script>
</body>
</html>
    
answered by 24.10.2018 в 13:54
1

You are using String.prototype.split () .

This function should be used as follows:

According to the mozilla documentation the function has the following syntax

cadena.split([separador][,limite])
     

Where

     

separator

     

Specifies the character to use for chain separation. The   Separator is treated as a string or as a regular expression. Yes   the separator is omitted, the returned array will contain a single element   with the full chain.

     

limit

     

Optional. Integer that specifies a limit on the number of   divisions to be made. The split () method is still divided in all   matches the separator, but divides the matrix returned in the   amount of elements imposed by the limit.

Taking this to your particular case, it is understood that having an error in the next line

var separa = nombre.split(".");

where the right thing would be

var separa = nombre.split(" ",2);

As you can see, use the function split() with 2 arguments, where, as the first parameter, a space will pass, since the name will be separated by a space, and as a second parameter it will pass an integer 2 , since I hope it's only two words that I receive.

So it would produce the following

nombre: Marcos Perez 
separa[0]: Marcos
separa[1]: Perez

nombre: Gonzalo Martinez Gomez
separa[0]: Gonzalo
separa[1]: Martinez
separa[2]: no existe debido al segundo parámetro de split()

Then, having already managed to separate the name and surname, it would only suffice to concatenate separa[0] with the first character of separa[1]

var resultado = separa[0] + " " + separa[1].charAt(0)+".";  
    
answered by 24.10.2018 в 13:59