My friend asked me to look at the code of JavaScript she did why it is wrong, I didnt learn JavaScript, so could anyone help, it should be very easy JavaScript code:
<!DOCTYPE html>
<html>
<head>
<title>
</title>
<script>
function getInputAsNumber(_id){return parseFloat(document.getElementById(_id).value)}
function setOutput(_id, _value){document.getElementById(_id).value = _value}
function calculate()
{
// declare a variable
var first;
var second;
var third;
var fourth;
var fifth;
var max;
var beta;
// get variable's value
first = getInputAsNumber("firstBox")
second = getInputAsNumber("secondBox")
third = getInputAsNumber("thirdBox")
fourth = getInputAsNumber("fourthBox")
fifth = getInputAsNumber("fifthBox")
// perform this
max=first;
if (max < second)
{
max = second
}
if (max<third)
{
max=third
}
if (max<forth)
{
max=forth
}
if (max<fifth)
{
max=fifth
}
// write Output value
setOutput("resultAsNumber",max);
}
</script>
</head>
<body>
Instruction:<br>
Type 5 numbers and click Go.<br>
The highest of the 5 will appear.<br>
It is okay to use a decimal point.<br>
<br>
Input Values:<br>
First <input id="firstBox"><br>
Second <input id="secondBox"><br>
Third <input id="thirdBox"><br>
Fourth <input id="fourthBox"><br>
Fifth <input id="fifthBox"><br>
<button onclick="calculate()">Go</button><br>
<br>
Max value:<document.write("resultAsNumber")>
</body>
</html>
// get variable's value
first = getInputAsNumber("firstBox");
second = getInputAsNumber("secondBox");
third = getInputAsNumber("thirdBox");
fourth = getInputAsNumber("fourthBox");
fifth = getInputAsNumber("fifthBox");
// perform this
max=first;
if (max < second)
{
max = second;
}
if (max<third)
{
max=third;
}
if (max<forth)
{
max=forth;
}
if (max<fifth)
{
max=fifth;
}
I don't really use Javascript but.. Max value:<document.write("resultAsNumber")>
This isn't going to do the correct thing (as has been mentioned here before).
In the calculate function it is trying to reference an element by the id "resultAsNumber" which hasn't been defined anywhere in html. You will want to replace that document.write("resultAsNumber") with something like. Max Value: <input type ="text" id ="resultAsNumber">
That is creating an input box with an id of resultAsNumber which can then be changed by the setOutput function above.
(Also the code is spelling fourth differently as times, double check those or it wont work).
It works correctly for me once I make both of those changes I mentioned (pretty sure the ; is optional as I don't add them, but it doesn't hurt to have them).
Make sure you change both of the spelling mistakes so it looks like this.
1 2 3 4
if (max<fourth)
{
max=fourth
}
The "text" just specifies it's a textbox but it doesn't have to be included.
If it still doesn't work after you have double checked that stuff, post again or edit your post and I can look.
<!DOCTYPE html>
<html>
<head>
<title>
</title>
<script>
function getInputAsNumber(_id){return parseFloat(document.getElementById(_id).value)};
function setOutput(_id, _value{document.getElementById(_id).value = _value};
function calculate()
{
// declare a variable
var first;
var second;
var third;
var fourth;
var fifth;
var max;
var beta;
// get variable's value
first = getInputAsNumber("firstBox");
second = getInputAsNumber("secondBox");
third = getInputAsNumber("thirdBox");
fourth = getInputAsNumber("fourthBox");
fifth = getInputAsNumber("fifthBox");
// perform this
max=first;
if (max < second)
{
max = second;
}
if (max<third)
{
max=third;
}
if (max<fourth)//changed
{
max=fourth;//changed
}
if (max<fifth)
{
max=fifth;
}
// write Output value
setOutput("resultAsNumber",max);
}
</script>
</head>
<body>
Instruction:<br>
Type 5 numbers and click Go.<br>
The highest of the 5 will appear.<br>
It is okay to use a decimal point.<br>
<br>
Input Values:<br>
First <input id="firstBox"><br>
Second <input id="secondBox"><br>
Third <input id="thirdBox"><br>
Fourth <input id="fourthBox"><br>
Fifth <input id="fifthBox"><br>
<button onclick="calculate()">Go</button><br>
<br>
Max value: <input type ="text" id ="resultAsNumber"> //changed
</body>
</html>
Because I am studying C++, like I just said, it's my friend's code, I can only help her logic, but can't help the codes, i even didnt have the compiler to compile this JavaScript, I just pick up an online instant compiler to run the program!
Thank you very much for your guys help! I really appreciate!