-
Notifications
You must be signed in to change notification settings - Fork 63
/
Catalog.js
35 lines (26 loc) · 1.29 KB
/
Catalog.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
/*
Description:
You are given a small extract of a catalog:
s = "<prod><name>drill</name><prx>99</prx><qty>5</qty></prod>
<prod><name>hammer</name><prx>10</prx><qty>50</qty></prod>
<prod><name>screwdriver</name><prx>5</prx><qty>51</qty></prod>
<prod><name>table saw</name><prx>1099.99</prx><qty>5</qty></prod>
<prod><name>saw</name><prx>9</prx><qty>10</qty></prod>
...
(prx stands for price, qty for quantity) and an article i.e "saw".
The function catalog(s, "saw") returns the line(s) corresponding to the article with $ before the prices:
"table saw > prx: $1099.99 qty: 5\nsaw > prx: $9 qty: 10\n..."
If the article is not in the catalog return "Nothing".
Notes
There is a blank line between two lines of the catalog.
The same article may appear more than once. If that happens return all the lines concerned by the article (in the same order as in the catalog).
The line separator of results may depend on the language \nor \r\n. You can see examples in the "Sample tests".
*/
function catalog(s, article) {
return s.match(new RegExp(`name.+${article}.+\/qty`,'g'))?s.match(new RegExp(`name.+${article}.+\/qty`,'g'))
.map(v=>v.replace(/<\/name><prx>/,' > prx: $'))
.map(v=>v.replace(/name>/,''))
.map(v=>v.replace(/<\/prx><qty>/,' qty: '))
.map(v=>v.replace(/<\/qty/,'')).join`\r\n`
:'Nothing'
}