Question: How can I display this script file in HTML? I have two .js files and I need the result displayed on an HTML page. Below
How can I display this script file in HTML?
I have two .js files and I need the result displayed on an HTML page. Below is the code I have so far.
index.js
// Import the findLargerNumber function from the larger.js file.
const findLargerNumber = require('./larger');
// Define two numbers to pass to the findLargerNumber function.
let num1 = prompt("Enter first number.");
let num2 = prompt("Enter second number.");
// Call the findLargerNumber function with the two numbers and store the result in a variable.
const largerNum = findLargerNumber(num1, num2);
// Display the result in the console.
console.log(`${largerNum} is the larger number`);
larger.js
// Define a function that takes two numbers and returns the larger of the two.
function findLargerNumber(num1, num2) {
if (num1 > num2) {
return num1;
} else {
return num2;
}
}
// Export the function so it can be used in other files.
module.exports = findLargerNumber
index.html
console.log(`${largerNum} is the larger number`);
//This is the statement I cant get to print out.
What the outcome should look like: , if 5 and 8 are used, the console will display 8 is the larger number 0 point assigned if not using module to export and import the function that finds the larger number.
Thank you for your time.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
