write a java script program that reads temp of user. If temp…

write a java script program that reads temp of user. If temperature is less than 10 it outputs “low”, if temperature is greater than 10<20 it outputs "moderate",if temperature is greater than 20<30 it outputs "high",if temperature is greater than 30 it outputs "too high"

Answer

To create a JavaScript program that reads the user’s temperature input and outputs different messages based on the temperature ranges, we can use conditional statements. Specifically, we will use the if-else statements to check the temperature and display the appropriate message. Here’s an example program:

“`javascript
// Read user’s temperature
var temperature = parseFloat(prompt(“Please enter your temperature:”));

// Check the temperature range using if-else statements
if (temperature < 10) { console.log("Low"); } else if (temperature >= 10 && temperature < 20) { console.log("Moderate"); } else if (temperature >= 20 && temperature < 30) { console.log("High"); } else { console.log("Too high"); } ``` Let's break down the program to understand how it works: 1. The `parseFloat` function is used to convert the user's input into a floating-point number because temperature values can have decimal places. 2. The `prompt` function displays a window with a message asking the user to enter their temperature. The user's input is then stored in the `temperature` variable. 3. The if-else statements are used to check the value of the `temperature` variable and determine the appropriate message to display. - If the temperature is less than 10, the program executes the code inside the first if-block and displays "Low". - If the temperature is between 10 and 20 (inclusive), the program executes the code inside the first else-if block and displays "Moderate". - If the temperature is between 20 and 30 (inclusive), the program executes the code inside the second else-if block and displays "High". - If the temperature is greater than or equal to 30, the program executes the code inside the else block and displays "Too high". 4. The `console.log` function is used to output the appropriate message to the console. Please note that in this program, the temperature ranges are inclusive. If you want to make them exclusive, you need to modify the comparison operators accordingly. You can run this JavaScript program by opening a text editor, pasting the code, and saving the file with a `.js` extension (e.g., `temperature.js`). Then, open the file in a web browser console or a JavaScript runtime environment to see the output.

Do you need us to help you on this or any other assignment?


Make an Order Now