diff --git a/src/index.js b/src/index.js index ca8a5cf..f464f54 100644 --- a/src/index.js +++ b/src/index.js @@ -8,10 +8,16 @@ // Import the factorial function from the factorial.js file const { factorial } = require('./utils/factorial'); -// The main() function calculates the factorial of 5 and logs the result to the console +const { convertToCelsius } = require('./utils/fahrenheit_to_celsius_converter'); + +// The main() function calculates the factorial of 5 & converts the fahrenheit temp of 50 to celsius, and logs both results to the console async function main () { const result = factorial(5); // Call the factorial function with the value of 5 console.log('factorial of 5 is', result); // Log the result to the console + + const fahrenheitTemp = 50; // Fahrenheit temp to convert to Celsius + const celsiusTemp = convertToCelsius(fahrenheitTemp); // Convered temperature + console.log('the Celsius temperature is:', celsiusTemp); // log to console } -main(); // Call the main function to calculate the factorial of 5 \ No newline at end of file +main(); // Call the main function to calculate the factorial of 5 & convert the fahrenheit temp of 50 \ No newline at end of file diff --git a/src/utils/fahrenheit_to_celsius_converter.js b/src/utils/fahrenheit_to_celsius_converter.js new file mode 100644 index 0000000..d305262 --- /dev/null +++ b/src/utils/fahrenheit_to_celsius_converter.js @@ -0,0 +1,25 @@ +/** + * Author: Kaitlyn Kelly + * Date: 6/3/2026 + * File: fahrenheit_to_celsius_converter.js + * Description: This script converts a fahrenheit temp to a celsius temp + */ +'use strict'; + +// function to convert a fahrenheit temp to equal temp in celsius +function convertToCelsius(n) { + + // if n is not a number, throw error and return + if (typeof n !== 'number' || Number.isNaN(n)) { + throw new Error('Input must be a valid number'); + } + + // formula to convert n to a celsius temp & store in celsiusTemp variable + const celsiusTemp = (n - 32) * (5/9); + + // round converted celsius temp to two decimal places & return value + return Math.round(celsiusTemp * 100) / 100; +} + +// Export the function for use in other scripts +module.exports = { convertToCelsius }; \ No newline at end of file diff --git a/test/utils/fahrenheit_to_celsius_converter.spec.js b/test/utils/fahrenheit_to_celsius_converter.spec.js new file mode 100644 index 0000000..31f61d4 --- /dev/null +++ b/test/utils/fahrenheit_to_celsius_converter.spec.js @@ -0,0 +1,30 @@ +/** + * Author: Kaitlyn Kelly + * Date: 6/3/2026 + * File: fahrenheit_to_celsius_converter.spec.js + * Description: This script tests the convertToCelsius function + */ +'use strict'; + +// import the convertToCelsius functon from its file +const { convertToCelsius } = require ('../../src/utils/fahrenheit_to_celsius_converter'); + +describe('fahrenheit_to_celsius_converter.js', () => { + + // test that the function correctly converts a temperature to celsius + it('should convert a fahrenheit temperature to its celsius equivalent', () => { + const result = convertToCelsius(100); + expect(result).toBe(37.78); + }); + + // test that the function converts a negative number + it('should convert a negative temperature to its celsius equivalent', () => { + const result = convertToCelsius(-5); + expect(result).toBe(-20.56); + }); + + // test that the function throws an error when a non-number is supplied + it('should throw an error when a non-number is supplied as the input', () => { + expect(() => convertToCelsius("ten")).toThrow('Input must be a valid number'); + }); +}); \ No newline at end of file