-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
68 lines (60 loc) · 1.82 KB
/
index.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
62
63
64
65
66
67
68
// Complete the following functions to make our program work!
/**
* Converts the given Fahrenheit temperature `f` to Celsius.
* @param {number} f temperature in °F
* @returns {number} temperature in °C
*/
function convertToCelsius(f) {
return (f - 32) * (5 / 9);
}
/**
* | Temperature | Description |
* | ----------- | ----------- |
* | < 32 | "very cold" |
* | < 64 | "cold" |
* | < 86 | "warm" |
* | < 100 | "hot" |
* | >= 100 | "very hot" |
*
* @param {number} f temperature in °F
* @returns {string} the description from the table above corresponding to
* the given Fahrenheit temperature `f`
*/
function describeTemperature(f) {
if (f < 32) return "very cold";
else if (f < 64) return "cold";
else if (f < 86) return "warm";
else if (f < 100) return "hot";
else return "very hot";
}
/**
* @param {number} limit
* @returns {number} a random integer in the range [0, `limit`)
*/
function getRandomInt(limit) {
return Math.round(Math.random() * 10);
}
// -------------------- DO NOT CHANGE THE CODE BELOW ---------------------- //
/**
* Converts the given temperature from Fahrenheit to Celsius,
* then alerts the user with a descriptive message.
* @param {number} f temperature in °F
*/
function parseFahrenheit(f) {
const c = convertToCelsius(f);
const description = describeTemperature(f);
const message = `${f}°F is ${c}°C. That is ${description}.`;
alert(message);
}
const fahrenheitPrompt =
"Please enter a number. We will convert that temperature from Fahrenheit to Celsius.";
let f = prompt(fahrenheitPrompt);
parseFahrenheit(+f);
alert("Let's try that again.");
f = prompt(fahrenheitPrompt);
parseFahrenheit(+f);
alert("Let's try some random temperatures.");
f = getRandomInt(110);
parseFahrenheit(f);
f = getRandomInt(110);
parseFahrenheit(f);