Troncate il testo
importanza: 5
Create una funzione truncate(str, maxlength) che controlla la lunghezza di str e, se questa eccede maxlength â rimpiazzate la fine di str con il carattere "â¦", in modo tale da ottenere una lunghezza pari a maxlength.
Come risultato la funzione dovrebbe troncare la stringa (se ce nâè bisogno).
Ad esempio:
truncate("What I'd like to tell on this topic is:", 20) = "What I'd like to teâ¦"
truncate("Hi everyone!", 20) = "Hi everyone!"
La lunghezza massima deve essere maxlength, quindi abbiamo bisogno di troncare la stringa, per fare spazio al carattere ââ¦â
Da notare che esiste un carattere Unicode che identifica ââ¦â. Questo non è lo stesso che usare tre punti.
function truncate(str, maxlength) {
return (str.length > maxlength) ?
str.slice(0, maxlength - 1) + 'â¦' : str;
}