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.
Write a program that asks the user for a Fahrenheit temperature and calls a function name...
Write a program that asks the user for a Fahrenheit temperature and calls a function name Celsius that returns the temperature in Celsius of the Fahrenheit temperature sent to it. Your function will look similar to this: double celsius(double f) { double c; : : return c; }
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  ...
*In C++ language please* Create a table that converts temperatures from Celsius to Fahrenheit Ask the...
*In C++ language please* Create a table that converts temperatures from Celsius to Fahrenheit Ask the user if they would like to know today's temperatures When "yes": Ask or their name -Generate a random number: use the length of their name (nameLngth) to set the seed Use the length of their name (nameLngth) as the starting point, and 99 as the ending point for a range of Celsius temperatures Output the range of Celsius temperatures alongside the Fahrenheit temperature conversions...
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 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...
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...
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.
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT