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.
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...
Palindrome Javascript I am trying to write this javascript function to check if a number is...
Palindrome Javascript I am trying to write this javascript function to check if a number is Palindrome.. Palindrome means - a word, phrase, or sequence that reads the same backward as forward, e.g., madam or nurses run. This is the code i have, but it doesnt work. Code: let convertButton = document.getElementsByClassName("btn")[0]; let userInput = document.getElementById("number").value; let results = document.getElementById("result").value; convertButton.addEventListener("click", function (event) { event.preventDefault(); console.log(userInput); if (validatePalidrome(userInput)) document.getElementById("result").innerHTML = "true"; else document.getElementById("result").innerHTML = "false"; }); function validatePalidrome(numbers) { let...
Palindrome Javascript - NUMBERS I am trying to write this javascript function to check if a...
Palindrome Javascript - NUMBERS I am trying to write this javascript function to check if a number is Palindrome.. Palindrome means - a word, phrase, or sequence that reads the same backward as forward, e.g., 12321 This is the code i have, but it doesnt work. PLEASE MAKE SURE IT WORK BEFORE POST ANSWER, I GOT @ CODE THAT DID WORK BEFORE HTML JavaScript Palindrome Exercise rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.4.1/css/bootstrap.min.css" /> Palindrome Enter a positive number: Is this a palindrome? No...
Create a JavaScript function that will collect the information from the form and verify that it...
Create a JavaScript function that will collect the information from the form and verify that it is the correct type and that there are no blank textboxes. Save and test the file to ensure that the textbox information is collected and the script is working correctly. Use the onclick event within the submit button to call this function. Output validation error messages by writing directly to the with the id of "results." (You may want to use alert boxes for...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT