forked from HackYourFuture/JavaScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest scripts.js
More file actions
50 lines (42 loc) · 1.3 KB
/
test scripts.js
File metadata and controls
50 lines (42 loc) · 1.3 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
38
39
40
41
42
43
44
45
46
47
48
49
50
const companies = [
{
name: "Acme Corp",
ceo: {
name: "John Doe",
location: { city: "New York", country: "USA" },
},
},
{
name: "Beta Ltd",
ceo: null, // No CEO for this company
},
{
name: "Gamma Inc",
ceo: { name: "Jane Smith" }, // CEO without location information
},
{
name: "Kojima Productions",
ceo: {
name: "Hideo Kojima",
location:{city: "Tokyo", country:"Japan"}
}
},
];
// Fix the function to safely retrieve the CEO's city or a default message
function getCeoCity(company) {
if (company.ceo) {
if (company.ceo.location) {
return company.ceo.location.city;
} else {
return "No city information available";
}
} else {
return "No city information available";
}
// const city = company.ceo?.location?.city || "No city information available";
// return city
}
console.log(getCeoCity(companies[0])); // Should output "New York"
console.log(getCeoCity(companies[1])); // Should output "No city information available"
console.log(getCeoCity(companies[2])); // Should output "No city information available"
console.log(getCeoCity(companies[3])); // Should output "No city information available"