
function test() {
const temperature = parseFloat(prompt("Enter the temperature in degrees Celsius"))
if (temperature < -10) {
alert("It's very cold");
} else if ((temperature >= -10) && (temperature < 0)) {
alert("It's cold");
} else if ((temperature >= 0) && (temperature < 10)) {
alert("It's cool");
} else if ((temperature >= 10) && (temperature < 20)) {
alert("It's warm")
} else{
alert("It's hot")
}
}Conditional statements are a fundamental concept in programming. They allow applications to make decisions based on specific conditions and execute different blocks of code depending on the values provided.
In this JavaScript exercise, we create a simple application that asks the user to enter a temperature in degrees Celsius and classifies it into five different categories using if, else if and else statements. We also explore comparison operators, the logical AND operator and the conversion of strings into floating point numbers.
The JavaScript code
function test() {
const temperature = parseFloat(prompt("Enter the temperature in degrees Celsius"));
if (temperature < -10) {
alert("It's very cold");
} else if ((temperature >= -10) && (temperature < 0)) {
alert("It's cold");
} else if ((temperature >= 0) && (temperature < 10)) {
alert("It's cool");
} else if ((temperature >= 10) && (temperature < 20)) {
alert("It's warm");
} else {
alert("It's hot");
}
}
1. Requesting input from the user
The application starts by asking the user to enter a temperature:
const temperature = parseFloat(prompt("Enter the temperature in degrees Celsius"));
The prompt() function displays a dialog box in the browser, allowing the user to enter a value.
By default, the prompt() function returns the entered value as a string. Since we want to perform numerical comparisons, we use the parseFloat() function to convert the string into a floating point number.
Unlike parseInt(), the parseFloat() function preserves decimal values.
For example:
parseFloat("15.7"); // 15.7
parseInt("15.7"); // 15
This allows our application to process temperatures containing decimal values accurately.
2. Understanding conditional statements
The application uses a combination of if, else if and else statements to determine which temperature category applies.
The conditions are evaluated sequentially. As soon as a condition evaluates to true, its corresponding block of code is executed, and the remaining conditions are skipped.
The following temperature categories are defined in our application:
| Temperature range | Classification |
|---|---|
| Below −10°C | Very cold |
| −10°C to below 0°C | Cold |
| 0°C to below 10°C | Cool |
| 10°C to below 20°C | Warm |
| 20°C and above | Hot |
3. Using comparison operators
Comparison operators allow us to compare numerical values and determine whether a condition is true or false.
In this exercise, we use the following comparison operators:
| Operator | Description |
|---|---|
< | Less than |
>= | Greater than or equal to |
For example:
if (temperature < -10) {
alert("It's very cold");
}
If the temperature is below −10°C, the condition evaluates to true, and the corresponding message is displayed.
4. Understanding the logical AND operator
The logical AND operator, represented by &&, allows us to combine multiple conditions into a single expression.
Consider the following statement:
else if ((temperature >= -10) && (temperature < 0)) {
alert("It's cold");
}
Both conditions must evaluate to true before the corresponding block of code is executed.
For example, if the user enters −5°C:
temperature >= -10 // true
temperature < 0 // true
The combined expression evaluates to true.
The logical AND operator follows this truth table:
| Condition A | Condition B | Result |
|---|---|---|
| true | true | true |
| true | false | false |
| false | true | false |
| false | false | false |
5. Displaying the result
The alert() function displays a dialog box containing the result of the temperature classification.
alert("It's warm");
For example, when the user enters 15°C, the application displays the message “It’s warm”.
7. Testing the application using boundary value analysis
From a software testing perspective, this exercise is an excellent example of boundary value analysis, a testing technique that focuses on values at and around the boundaries of different input ranges.
The boundaries in our application are −10°C, 0°C, 10°C and 20°C.
| Test input | Expected result |
|---|---|
| −10.1 | Very cold |
| −10 | Cold |
| −9.9 | Cold |
| −0.1 | Cold |
| 0 | Cool |
| 0.1 | Cool |
| 9.9 | Cool |
| 10 | Warm |
| 10.1 | Warm |
| 19.9 | Warm |
| 20 | Hot |
| 20.1 | Hot |
Testing these values helps verify that the comparison operators have been implemented correctly and that there are no gaps or overlaps between the temperature categories.
8. Improving the code with input validation
The original implementation contains one important limitation. If the user enters an invalid value, such as "ABC", the parseFloat() function returns NaN.
Since all numerical comparisons with NaN evaluate to false, the application incorrectly displays “It’s hot”.
We can improve the application by validating the input before classifying the temperature.
We can also simplify the conditional statements by removing redundant comparisons. Because the conditions are evaluated sequentially, we do not need to check the lower boundary of every temperature range.
function test() {
const temperature = parseFloat(prompt("Enter the temperature in degrees Celsius"));
if (!Number.isFinite(temperature)) {
alert("Please enter a valid temperature.");
} else if (temperature < -10) {
alert("It's very cold");
} else if (temperature < 0) {
alert("It's cold");
} else if (temperature < 10) {
alert("It's cool");
} else if (temperature < 20) {
alert("It's warm");
} else {
alert("It's hot");
}
}
This version is shorter, more readable and handles invalid numerical results, empty input, cancelled prompts and infinite values.
However, parseFloat() still accepts partially numerical strings such as "15ABC" and converts them into 15. A production application requiring strict input validation should also validate the complete input string.
Conclusion
This JavaScript exercise demonstrates several essential programming concepts:
- Requesting user input using
prompt(). - Converting strings into floating point numbers using
parseFloat(). - Making decisions using conditional statements.
- Comparing numerical values using comparison operators.
- Combining conditions using the logical AND operator.
- Displaying messages using
alert(). - Validating numerical input.
- Applying boundary value analysis to test application behaviour.
The underlying principles are widely applicable in software development. Similar conditional structures are used in pricing systems, industrial automation, environmental monitoring, business applications and countless other software solutions.
Understanding how to implement, simplify and test conditional statements is an important step towards writing reliable and maintainable JavaScript applications.