Question

In: Computer Science

Write a javascript program according to the follow requirements: Create a function that converts Fahrenheit to...

Write a javascript program according to the follow requirements:

  • Create a function that converts Fahrenheit to Celsius. It takes a single argument which represents degrees in Fahrenheit. It converts it and returns the degrees in Celsius.
  • Create another function that converts Celsius to Fahrenheit. It takes a argument in Celsius and returns the degrees in Fahrenheit.
  • Implement the function convert(isFtoC, from, to) below. It takes the following three arguments:
    • isFtoC: a boolean that is true if degrees must be converted from Fahrenheit to Celsius or false if degrees must be converted from Celsius to Fahrenheit
    • from: this represents the first value in a range of degrees that will be converted
    • to: this represents the last value in a range of degrees to be converted
  • The function must convert every degree in the specified range and show each degree and the converted result on the page. Call show(str) to display a string on the page.

function convert(isFtoC, from, to) {
  // your code here
}

Note: Use .toFixed(2) to limit the decimal digits to 2, e.g. num.toFixed(2)

Solutions

Expert Solution

Dear Student ,

As per the requirement submitted above , kindly find the below solution.

Here a new web page with name "tempConversion.html" is created, which contains following code.

tempConversion.html :

<!DOCTYPE html>

<html lang="en">

<head>

<!-- title for web page -->

<title>Temprature Conversion</title>

<meta charset="UTF-8">

<meta name="viewport" content="width=device-width, initial-scale=1">

</head>

<body>

From Temp :<input type="text" id="txtFrom"/>

<br><br>

To Temp :<input type="text" id="txtTo"/>

<br><br>

Select Conversion :

<select id="temp">

<option selected>--Select Conversion--</option>

<option value="Fahrenheit">Fahrenheit</option>

<option value="Celsius">Celsius</option>

</select>

<br><br>

<input type="button" onclick="convertTemp()" value="Convert"/>

<!-- div to display temprature -->

<div id="myDiv"></div>

<!-- <script> is used for javascript -->

<script>

//function to convert temprature

function convertTemp()

{

var from=parseFloat(document.getElementById("txtFrom").value);//take from temp

var to=parseFloat(document.getElementById("txtTo").value);//take to temp

//checking what is selected

if(document.getElementById("temp").value=="Fahrenheit")

{

//if Fahrenheit is selected

convert(true,from,to);

}

else{

//if Celisus is selected

convert(false,from,to);

}

}

//function to convert Fahrenheit to Celsius

function fToC(f)

{

return (f-32)* 5/9;//return temp in c

}

//function to convert Celsius to Fahrenheit

function cTof(c)

{

return (c*9/5)+32;//return temp in f

}

//function to convert temprature

function convert(isFtoC, from, to)

{

var tempStr="";

//checking if Fahrenheit to Celsius

if(isFtoC==true)

{ tempStr="F:C<br/>";

//convert temprature in Fahrenheit

for(var i=from;i<=to;i++)

{

tempStr+=i+" : "+fToC(i).toFixed(2)+"<br/>";//call function and concate

}

show(tempStr);//call function to display details

}

else if(isFtoC==false){

tempStr="C:F<br/>";

for(var i=from;i<=to;i++)

{

tempStr+=i+" : "+cTof(i).toFixed(2)+"<br/>";//call function and concate

}

show(tempStr);//call function to display details

}

}

//function to show string

function show(str)

{

document.getElementById("myDiv").innerHTML=str;//display on the div

}

</script>

</body>

</html>

======================================================

Output : Compile and Run tempConversion.html in the browser and will get the screen as shown below

Screen 1 :tempConversion.html

Screen 2 :Temp from f to c

Screen 3 :Temp from c to f

NOTE : PLEASE FEEL FREE TO PROVIDE FEEDBACK ABOUT THE SOLUTION.


Related Solutions

Instructions Write a program in C# that converts a temperature given in Fahrenheit to Celsius. Allow...
Instructions Write a program in C# that converts a temperature given in Fahrenheit to Celsius. Allow the user to enter values for the original Fahrenheit value. Display the original temperature and the formatted converted value. Use appropriate value returning methods for entering (input), calculating, and outputting results.
(Javascript) Write a function that converts the alphabetical characters of first half of each argument to...
(Javascript) Write a function that converts the alphabetical characters of first half of each argument to uppercase and returns the result as one string. Number/s and special chars are ignored. If the length of string is not even, append an extra "-" (hyphen) to end of it to make it even.
Program Requirements: Write a C++ program according to the following requirements: 1.   Open the data file...
Program Requirements: Write a C++ program according to the following requirements: 1.   Open the data file Electricity.txt and read each column into an array (8 arrays total). 2.   Also create 2 arrays for the following: Total Fossil Fuel Energy (sum of all fossil fuels) Total Renewable Energy (sum of all renewable sources) Electricity.txt: Net generation United States all sectors monthly https://www.eia.gov/electricity/data/browser/ Source: U.S. Energy Information Administration All values in thousands of megawatthours Year   all fuels   coal       natural gas   nuclear  ...
Write the below code to use HTML and JavaScript. 1. a) Write a JavaScript program to...
Write the below code to use HTML and JavaScript. 1. a) Write a JavaScript program to display the current day and time. b) Write a JavaScript program to print the contents of the current window.   c) Write a JavaScript program where the program takes a random integer between 1 to 10 d) Write a JavaScript program to calculate multiplication and division of two numbers (input from the user). e)Write a JavaScript program to create a new string from a given...
Language Python with functions and one main function Write a program that converts a color image...
Language Python with functions and one main function Write a program that converts a color image to grayscale. The user supplies the name of a file containing a GIF or PPM image, and the program loads the image and displays the file. At the click of the mouse, the program converts the image to grayscale. The user is then prompted for a file name to store the grayscale image in.
Write a simple javascript program using express and node.js to create a simple webpage that can...
Write a simple javascript program using express and node.js to create a simple webpage that can lead to other pages within the program. if possible, Comment everything out so I could understand how every things works.  But basically, server should be hosted on localhost:8000 and should have a simple starting page. Maybe a welcome message, with at least two "links" that goes to a "homepage" and then a "exit" page.
XML and JAVA Write a Java program that meets these requirements. It is important you follow...
XML and JAVA Write a Java program that meets these requirements. It is important you follow these requirements closely. • Create a NetBeans project named LastnameAssign1. Change Lastname to your last name. For example, my project would be named NicholsonAssign1. • In the Java file, print a welcome message that includes your full name. • The program should prompt for an XML filename to write to o The filename entered must end with .xml and have at least one letter...
USING JAVASCRIPT Create a file name dayOfWeek.js and write an arrow function named dayOfWeek that accepts...
USING JAVASCRIPT Create a file name dayOfWeek.js and write an arrow function named dayOfWeek that accepts a Date object dateStr and returns a string that is the day of the week in English form (i.e. “Sunday”, “Monday”, etc.). Test your function by creating a date object that is a significant date to you (such as your birthday) and passing that date object to your function. Test your function at least twice with two different dates. Submit the dayOfWeek.js file to...
Write a JavaScript program with a function named fives that reads two numbers from two text...
Write a JavaScript program with a function named fives that reads two numbers from two text fields and then outputs to a div "True" if both of the numbers are greater than 5 or if their sum is greater than 20. Otherwise your function should output "False" to the div. If you wish, you may use the following HTML code to begin your program.
This is C++ Create a program that reads an HTML file and converts it to plain...
This is C++ Create a program that reads an HTML file and converts it to plain text. Console: HTML Converter Grocery List * Eggs * Milk * Butter Specifications: The HTML file named groceries.html contains these HTML tags: <h1>Grocery List</h1> <ul> <li>Eggs</li> <li>Milk</li> <li>Butter</li> </ul> When the program starts, it should read the contents of the file, remove the HTML tags, remove any spaces to the left of the tags, add asterisks (*) before the list items, and display the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT