Click here to Skip to main content
15,891,902 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I was assigned to create a functions and events to ask user for length and width then compute for Perimeter and Area of rectangle.

The output should look like this: https://i.imgur.com/hAkQn9s.jpg

What I have tried:

<html>
<head><title>Forms</title></head>
<body>

<script type="text/JavaScript">
 function area(length, width) {
            return length * width;
        }
        function perimeter(length, width) {
            return 2*( length + width);
        }
</script>
<form name="Rectangle">
Enter the width and length then click <input type="button" name="answer" value="calculate" onClick='area()' ><br><br>

Length<input type="text" name="length" value="0" ><br><br>
Width<input type="text" name="width" value="0" ><br><br>
Area<input type="text" name="area" value="0" ><br><br>
Perimeter<input type="text" name="perimeter" value="0" ><br><br>





</form>
</body>
</html>
Posted
Updated 9-May-21 17:17pm
v2
Comments
enhzflep 9-May-21 23:47pm    
Exactly as you have successfully done here twice. Those functions are fine. What isn't fine though, is the way you're using them. You need to call both functions in response to the button press. The functions take input and return output. It's still up to you to feed them input and do something with the output, like showing the user.


Make another function. Call *it* in response to the button press. Inside this new function: retrieve the length and width from the inputs, receive the output and display it. It'd be a good idea to give all of your controls an ID, so you can access their values with document.getElementById("idGoesHere").value - both for retrieving and setting a value.
Jayson Tatum 10-May-21 0:37am    
I'm fairly new to JavaScript and I'm still a bit confused but thanks
enhzflep 10-May-21 0:55am    
If you haven't been put onto them yet, make all your trips for documentation to MDN (Mozilla Developer Network).

They document and provide examples for more things than you or I could probably ever think of.


I'd consider looking for a small tutorial. Something that talks about retrieving user-entered data from html elements. I tried to find one I thought worthy of recommendation and instead found this stack overflow posts, which seems to have potential for being helpful.

https://stackoverflow.com/questions/11563638/how-do-i-get-the-value-of-text-input-field-using-javascript


Lastly, if you add the disabled attribute to the input elements used to display the area and perimeter, they'll be greyed-out and will prevent the user typing data into what is actually an output element. (Shouldn't use inputs like this, the screenshot you provided appears to demonstrate mis-use of elements)


<input id='lengthIn', type='number'/>


versus

<input id='perimOut' disabled/>




Adding the type='number' attribute gives you up/down arrows you can click.

You might want to use parseFloat on the *text-string* retrieved from the inputs. Failing to do so has fun (not fun!) results.

During a test-run, I played with the numbers 2 and 3. Clearly, a perimeter of 10 should result. I got 46....
"2" + "3" = 23
2 * 23 = 46

parseFloat("2") + parseFloat("3") = 5

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900