Temperature Converter Objective To practice using JavaScript expressions, control structures, and operators Instructions For this lab you will need to create a javascript script which will prompt a user to enter a temperature in either Fahrenheit or Celsius. Your script will then need to determine which type of temperature was entered and then convert the temperature to the other units and display that converted temperature for the user. When the user is prompted to enter a temperature with a prompt() statement the user should be told to enter the units following the temperature as either F or C (for example 98.6F). Your program will then need to determine which was entered. If the user neglects to specify a unit, an error can be displayed in place of the converted temperature. To perform the conversion from Celsius to Fahrenheit: Multiply the Celsius temperature time 1.8 and add 32 (F = C * 1.8 + 32) To perform the conversion from Fahrenheit to Celsius: Subtract 32 from the Fahrenheit temperature and then Multiply times .55 (C = (F – 32) * .55)

Temperature Converter

Objective
The objective of this lab is to practice using JavaScript expressions, control structures, and operators by creating a script that converts temperatures between Fahrenheit and Celsius.

Instructions
In this lab, you will be creating a JavaScript script that prompts the user to enter a temperature in either Fahrenheit or Celsius. Your script should then determine the type of temperature entered and convert it to the other unit. The converted temperature should be displayed for the user.

When prompting the user to enter a temperature, use the prompt() statement and instruct them to enter the units following the temperature as either “F” or “C” (e.g., 98.6F). Your program needs to determine which unit was entered. If the user neglects to specify a unit, an error message can be displayed instead of the converted temperature.

Conversion Formula: Celsius to Fahrenheit
To convert Celsius to Fahrenheit, multiply the Celsius temperature by 1.8 and add 32.
Fahrenheit = Celsius * 1.8 + 32

Conversion Formula: Fahrenheit to Celsius
To convert Fahrenheit to Celsius, subtract 32 from the Fahrenheit temperature and then multiply by 0.55.
Celsius = (Fahrenheit – 32) * 0.55

Approach
To solve this problem, the script needs to perform the following steps:

1. Prompt the user to enter a temperature with the specified units (F or C).
2. Determine the entered temperature and its unit using control structures (e.g., if…else statements).
3. Based on the determined unit, apply the appropriate conversion formula.
4. Display the converted temperature to the user.

By following this approach, the script will be able to handle both Fahrenheit to Celsius and Celsius to Fahrenheit conversions based on the user’s input.

Code Implementation:
Below is an example implementation of the temperature converter script in JavaScript:

“`javascript
// Prompt the user to enter a temperature with units
var temperature = prompt(“Enter a temperature with units (e.g., 98.6F):”);

// Determine the entered temperature and its unit
var type = temperature[temperature.length – 1];
var value = parseFloat(temperature);

// Check the unit and convert accordingly
if (type === “F”) {
var celsius = (value – 32) * 0.55;
console.log(“Celsius: ” + celsius.toFixed(2));
} else if (type === “C”) {
var fahrenheit = value * 1.8 + 32;
console.log(“Fahrenheit: ” + fahrenheit.toFixed(2));
} else {
console.log(“Invalid input. Please enter the temperature in the correct format.”);
}
“`

Conclusion
By creating a JavaScript script that prompts the user for a temperature and converts it between Fahrenheit and Celsius, this lab allows for the practice of JavaScript expressions, control structures, and operators. The script successfully converts the temperature based on the user’s input, demonstrates the use of control structures, and provides an error message for invalid inputs.

Need your ASSIGNMENT done? Use our paper writing service to score better and meet your deadline.


Click Here to Make an Order Click Here to Hire a Writer