[codewars]Who likes it?

Who likes it?

  1. array templates
  2. Math.min( )
  3. replace( ) 搭配 正則
  4. 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
	})

}