-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcitiesVisited.js
executable file
·61 lines (47 loc) · 2.21 KB
/
citiesVisited.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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
// Description:
// Lucy loves to travel. Luckily she is a renowned computer
// scientist and gets to travel to international conferences
// using her department's budget.
// Each year, Society for Exciting Computer Science Research (SECSR)
// organizes several conferences around the world.
//Lucy always picks
// one conference from that list that is hosted in a city she hasn't
// been to before, and if that leaves her with more than one option,
// she picks the conference that she thinks would be most relevant
// for her field of research.
// Write a function conferencePicker that takes in two arguments:
// citiesVisited, a list of cities that Lucy has visited before, given
// as an array of strings.
// citiesOffered, a list of cities that will host SECSR conferences
// this year, given as an array of strings. citiesOffered will already
// be ordered in terms of the relevance of the conferences for Lucy's
// research (from the most to the least relevant).
// The function should return the city that Lucy should visit, as a
// string.
// Also note:
// You should allow for the possibility that Lucy hasn't visited any
// city before.
// SECSR organizes at least two conferences each year.
// If all of the offered conferences are hosted in cities that Lucy
// has visited before, the function should return 'No worthwhile conferences
// this year!' (Nothing in Haskell)
// Example:
// citiesVisited = ['Mexico City','Johannesburg','Stockholm','Osaka','Saint Petersburg','London'];
// citiesOffered = ['Stockholm','Paris','Melbourne'];
// conferencePicker (citiesVisited, citiesOffered);
// // ---> 'Paris'
citiesVisited = ['Mexico City', 'Johannesburg', 'Stockholm', 'Osaka', 'Saint Petersburg', 'London'];
citiesOffered = ['Stockholm', 'Paris', 'Melbourne', 'Madrid'];
function conferencePicker(citiesVisited, citiesOffered) {
if (citiesVisited.length === 0) {
return citiesOffered[0];
};
for (var city in citiesOffered) {
var currentCity = citiesOffered[city];
if (citiesVisited.indexOf(currentCity) === -1) {
return currentCity;
};
};
return "No worthwhile conferences this year!";
};
console.log(conferencePicker(citiesVisited, citiesOffered)); // ---> 'Paris'