Interactive Temperature Converter
Overview and Objectives
In this lab, learners will apply JavaScript operators (assignment, arithmetic, logical, comparison, and string operators) along with user interaction techniques to build a simple temperature converter. By the end of this lab, learners will be able to:
- Use
prompt()
to obtain user input. - Convert input strings to numbers.
- Apply arithmetic operators to convert Celsius to Fahrenheit using the formula:
Fahrenheit = (Celsius * 9/5) + 32
- Use conditional statements to validate user input.
- Output results clearly using
alert()
orconsole.log()
. - Write clean, well-documented code that demonstrates best practices.
Tasks and Instructions
Task 1: Create the Temperature Converter Script
-
File Creation:
- Create a JavaScript file named
tempConverter.js
.
- Create a JavaScript file named
-
User Input:
- Use the
prompt()
function to ask the user to enter a temperature in Celsius.
Example:let celsiusInput = prompt("Enter temperature in Celsius:");
- Use the
-
Input Validation and Conversion:
- Convert the input to a number using
Number()
or the unary plus (+
) operator. - Validate that the input is a valid number. If it is not, display an error message using
alert()
and terminate further execution.
- Convert the input to a number using
-
Perform Calculation:
- Use arithmetic operators to compute the Fahrenheit equivalent using the formula:
let fahrenheit = (celsius * 9/5) + 32;
- Use assignment operators appropriately during the calculation.
- Use arithmetic operators to compute the Fahrenheit equivalent using the formula:
-
Display the Result:
- Display the final result in a formatted message using either
alert()
orconsole.log()
. For example:
"25°C is equivalent to 77°F"
- Consider using template literals for clearer formatting.
- Display the final result in a formatted message using either
-
Enhancements:
- Provide additional user interaction, such as asking if the user wants to perform another conversion.
- Use additional error handling for edge cases (e.g., empty input).
Task 2: Test Your Script
- Run your script in a browser (using an HTML file that includes
tempConverter.js
) or using an online JavaScript editor. - Verify that valid numeric input produces the correct Fahrenheit conversion.
- Verify that non-numeric input triggers an error message.
Submission Criteria
The following should be included within a GitHub repository, and submitted via Canvas:
-
Files/Links:
- Submit the
tempConverter.js
file. - If using an online editor, include a link to your project.
- Submit the
-
Evidence of Output:
- Provide a screenshot or text log showing the code execution with both valid and invalid inputs.
-
Error-Free Code:
- Ensure the script runs without syntax or runtime errors.
Grading Rubric
Criteria | Points | Description |
---|---|---|
User Input & Prompting | 5 | Uses prompt() to obtain a temperature input from the user. |
Type Conversion & Input Validation | 5 | Correctly converts input to a number and validates that it is numeric; displays an error for invalid input. |
Arithmetic Calculation | 10 | Correctly applies the formula (Celsius * 9/5) + 32 to compute the Fahrenheit value using arithmetic operators. |
Conditional Logic | 5 | Uses an if statement to check for valid numeric input and handle errors appropriately. |
Output Formatting | 5 | Displays a clear, formatted message with both Celsius and Fahrenheit values using alert() or console.log() . |
Code Quality and Organization | 5 | Code is well-organized, properly indented, and includes meaningful variable names and inline comments. |
Enhancements | 5 | Additional features (e.g., loop for repeated conversions) are implemented and function correctly, demonstrating extra effort. |
Total Points: 40 points
Partial credit will be awarded for submissions that demonstrate an understanding of the core concepts, even if some components are incomplete or contain minor errors. For instance, if the arithmetic calculation is correct but the input validation is not robust, points will be deducted proportionally. Credit will be given for correct use of operators, user interaction, and overall code organization, even if enhancements are missing or there are minor syntax issues.