-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
79 lines (71 loc) · 2.03 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
69
70
71
72
73
74
75
76
77
78
79
// 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) {
let c = (f - 32) * 5 / 9;
return Math.round(c);
}
/**
* | 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) {
let description = "";
if (f < 32) {
description = "very cold";
} else if (f < 64) {
description = "cold";
} else if (f < 86) {
description = "warm";
} else if (f < 100) {
description = "hot";
} else {
description = "very hot";
}
return description;
}
/**
* @param {number} limit
* @returns {number} a random integer in the range [0, `limit`)
*/
function getRandomInt(limit) {
let RandomNumber = 0;
RandomNumber = Math.round(Math.floor(Math.random() * limit));
return RandomNumber;
}
// -------------------- 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);