-1
Hello I am wanting to write a function to reduce names by passing a limit, for example: limit 10 to the name: Sara Flávia Cristina to Sara F. C.
I wrote the code below, but I wonder if there’s a simpler way to do that.
Thank you.
let name = "Sabrina Boing Moreira";
let limit = 13;
verifyName(name, limit);
function verifyName(name, limit) {
let offLimit = isOffLimit(name, limit);
if(offLimit > 0) {
let namesList = splitName(name);
let reducedName = reduceName(namesList, limit, offLimit);
print(reducedName);
}
}
function isOffLimit(name, limit) {
if(name.length >= limit) {
return limit - name.length;
}
}
function splitName(name, limit) {
return name.split(" ", limit);
}
function reduceName(namesList, limit, offLimit) {
let name;
let reduceOfLimit = limit;
for (var i = 0; i < namesList.length; i++) {
name = countName(namesList[i]);
if(reduceOfLimit != 0) {
if(name.length + 1 == offLimit) {
namesList[i].slice(0, offLimit);
namesList[i] + ".";
reduceOfLimit = 0;
}
} else {
break
}
}
}
function countName(name) {
return name.length;
}
function print(string) {
console.log(string);
}
´´´
What does "limit 10" mean? And if this value were different, what would be the output, for example, to limit 15? And to limit 50? Or limit 5?
– Woss
I also did not understand the limit, is if the name is greater than the limit there abbreviates the surnames, if it is less or equal the name does not suffer change?
– Daniel Mendes
If you pass the limit, abbreviate if you do not keep the original text. @Woss
– Sabrina
And if you have "António de Sousa" or "Maria da Conceição d'Espada" as the abbreviated version?
– Sergio
would be Maria C. E. and Antonio S.
– Sabrina
As you would abbreviate "Pedro de Alcântara Francisco António João Carlos Xavier de Paula Miguel Rafael Joaquim José Gonzaga Pascoal Cipriano Serafim"?
– Victor Stafusa
@Victorstafusa D. Pedro I always breaks any test... :-)
– hkotsubo
Thanks for all your help, I was able to adapt my code based on your help.
– Sabrina