الأهمية: 5
قص النص
انشئ دالة باسم truncate(str, maxlength) تفحص طول النص str وتستبدل نهايته التي تتجاوز الحد maxlength بالرمز "…" لجعل طولها يساوي maxlength بالضبط. يجب أن تكون مخرجات الدالة النص المقصوص (في حال حدث ذلك). مثلًا:
truncate("What I'd like to tell on this topic is:", 20) = "What I'd like to te…"
truncate("Hi everyone!", 20) = "Hi everyone!"
The maximal length must be maxlength, so we need to cut it a little shorter, to give space for the ellipsis.
Note that there is actually a single Unicode character for an ellipsis. That’s not three dots.
function truncate(str, maxlength) {
return str.length > maxlength ? str.slice(0, maxlength - 1) + '…' : str;
}