-
Notifications
You must be signed in to change notification settings - Fork 3
/
Format.js
35 lines (28 loc) · 1.01 KB
/
Format.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
// Kata link:
// https://www.codewars.com/kata/53368a47e38700bd8300030d
// -------------------------------------
// Instructions:
/*
list([ {name: 'Bart'}, {name: 'Lisa'}, {name: 'Maggie'} ])
// returns 'Bart, Lisa & Maggie'
list([ {name: 'Bart'}, {name: 'Lisa'} ])
// returns 'Bart & Lisa'
list([ {name: 'Bart'} ])
// returns 'Bart'
list([])
// returns ''
*/
// -------------------------------------
// Solution:
function list(names) {
let ar = names.map((x) => {return x.name;});
let s = ar.slice(0, -1).join(", ");
return names.length > 1 ? s + " & " + ar.slice(-1) : ar.join("");
}
// -------------------------------------
// Basic Tests
console.log(list([{name: 'Bart'}, {name: 'Lisa'}, {name: 'Maggie'}, {name: 'Homer'}, {name: 'Marge'}]) == 'Bart, Lisa, Maggie, Homer & Marge')
console.log(list([{name: 'Bart'}, {name: 'Lisa'}, {name: 'Maggie'}]) == 'Bart, Lisa & Maggie')
console.log(list([{name: 'Bart'}, {name: 'Lisa'}]) == 'Bart & Lisa')
console.log(list([{name: 'Bart'}]) == 'Bart')
console.log(list([]) == '')