-
Notifications
You must be signed in to change notification settings - Fork 0
/
using-map.js
52 lines (45 loc) · 1.29 KB
/
using-map.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
function citiesOnly(arr){
return arr.map((obj) => obj["city"])
}
function upperCasingStates(arr) {
return arr.map(function(str) {
return str.replace(/\b\w/g, function(match) {
return match.toUpperCase()
})
})
}
function fahrenheitToCelsius(arr) {
return arr.map(function(str) {
var fahrenheit = parseFloat(str.replace("°F", ""))
var celsius = Math.floor((fahrenheit - 32 )* 5 / 9)
return celsius + "°C"
})
}
function trimTemp(arr) {
return arr.map(function(obj) {
var newObj = {
...obj,
temperature: obj.temperature.replace(/\s+/g, "").trim()
};
return newObj;
});
}
function tempForecasts(arr) {
return arr.map(function(element) {
let trimmed = trimTemp([element])[0]
let celsiusTemp = fahrenheitToCelsius([trimmed.temperature])[0].replace("°C" , "°Celsius")
let city = citiesOnly([element])[0]
let state = upperCasingStates([element.state])[0]
return `${celsiusTemp} in ${city}, ${state}`
});
}
console.log(
tempForecasts([
{
city: 'Pasadena',
temperature: ' 101 °F',
state: 'california',
region: 'West',
},
])
);// -> ['38°Celsius in Pasadena, California']