-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstringManipulation.js
More file actions
37 lines (28 loc) · 1.38 KB
/
stringManipulation.js
File metadata and controls
37 lines (28 loc) · 1.38 KB
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
36
37
// John has invited some friends. His list is:
// s = "Fred:Corwill;Wilfred:Corwill;Barney:Tornbull;Betty:Tornbull;Bjon:Tornbull;Raphael:Corwill;Alfred:Corwill";
// Could you make a program that
// makes this string uppercase
// gives it sorted in alphabetical order by last name.
// When the last names are the same, sort them by first name. Last name and first name of a guest come in the result between parentheses separated by a comma.
// So the result of function meeting(s) will be:
// "(CORWILL, ALFRED)(CORWILL, FRED)(CORWILL, RAPHAEL)(CORWILL, WILFRED)(TORNBULL, BARNEY)(TORNBULL, BETTY)(TORNBULL, BJON)"
// It can happen that in two distinct families with the same family name two people have the same first name too.
// Notes
// You can see another examples in the "Sample tests".
let s = "Fred:Corwill;Wilfred:Corwill;Barney:Tornbull;Betty:Tornbull;Bjon:Tornbull;Raphael:Corwill;Alfred:Corwill"
function meeting(s) {
let names = s.toUpperCase().split(";")
let alphaSortedNames = [];
for(let i=0; i<names.length; i++){
alphaSortedNames.push(names[i].split(":").reverse())
}
alphaSortedNames.sort();
let sortedLastFirst = alphaSortedNames.sort((a,b) => {
a[1].localeCompare(b[1]);
})
let finalSorted = sortedLastFirst.map((eachName) => {
return `(${eachName[0]}, ${eachName[1]})`
})
return finalSorted.join("").toString();
}
meeting(s);