JSFiddle - React, Tailwind, and code Playground
by fergmux
JavaScript
function getShortPetNames(pets) {
const maxNames = 3
let petNames = pets.map(pet => pet)
if (petNames.length > maxNames) {
const extraPetCount = petNames.length - maxNames + 1
petNames.splice(maxNames - 1, petNames.length, `${extraPetCount} more`)
console.log(petNames)
}
return petNames.slice(0, -1).join(', ') + ' and ' + petNames.slice(-1)
}
const x = [1]
console.log(getShortPetNames(x))