Who likes it?
- array templates
- Math.min( )
- replace( ) 搭配 正則
- array.shift( ) :
shift()
方法會移除並回傳陣列的第一個元素。
function a (names){
var template = [
"no one likes this",
"{name} likes this",
"{name} and {name} like this",
"{name}, {name} and {name} like this",
"{name}, {name} and {val} others like this"
]
let idx = Math.min(names.length, 4)
return template[idx].replace(/{name}|{val}/g, function(item){
return item==="{name}" ? names.shift() : names.length
})
}