I am trying to create an .html page that will allow for two string inputs (arguments) to be passed to the parameters of a .js function that will then return the longer of the 2 input values by comparing the string lengths.
First I wrote a .js that actually works while running it through VS CODE. Here it is:
function twoStrings(stringA, stringB) {
let longerStr;
if (stringA.length > stringB.length) {
return longerStr = stringA;
} else return longerStr = stringB;
}
console.log(twoStrings("three", "Baseball"))
Then I tried to create an .html file that will create the user interface. But I think there is an issue that at the end of each input line it is calling the function.
<!DOCTYPE html>
<head>
<title>Return Longest String.html</title>
</head>
<body>
<p>Please input two words:</p>
<input type="string" id="myInput1" oninput="twoStrings()">
<input type="string" id="myInput2" oninput="twoStrings()">
<p id="longerStr"> is the longer word</p>
<script type="text/javascript" src="twoStringsV1.js"></script>
</body>
</html>
and here is my .js
// 3. Write a function that takes in two strings and returns the longer of the two
function twoStrings(fval, sval) {
var fval = document.getElementById("myInput1").value;
var sval = document.getElementById("myInput2").value;
let longerStr;
if (fval.length > sval.length) {
return longerStr = fval;
} else return longerStr = sval;
}
document.getElementById("longerStr").innerHTML = longerStr;
One other thing is that at the end of my .html, where I am asking the .js to produce an innerHTML I am getting this line:
[object HTMLParagraphElement]
Thank you for your assistance.