admin管理员组文章数量:1312708
I'm creating a code in which the user can calculate the area of a rectangle. After inputting the height and width when prompted, the site should then print the height, width, and area values within a table. The height and width code works and prints properly, but the area will not print. Where am I going wrong?
Here is the separate JavaScript code.
var height = prompt("Please enter the height");
var width = prompt("Please enter the width");
function calcArea(height, width) {
var area = height * width;
return area;
}
Here is the HTML code in which the JavaScript's outputs are coded.
<table>
<caption>Calculate the Area of a Rectangle</caption>
<tr><td class="makert">Height:</td>
<td class="makectr"> <script>document.write(height);</script></td></tr>
<tr><td class="makert">Width:</td>
<td class="makectr"> <script>document.write(width);</script></td></tr>
<tr><td class="makert">Area:</td>
<td class="makectr"> <script>document.write(area);</script></td></tr>
</table>
I'm creating a code in which the user can calculate the area of a rectangle. After inputting the height and width when prompted, the site should then print the height, width, and area values within a table. The height and width code works and prints properly, but the area will not print. Where am I going wrong?
Here is the separate JavaScript code.
var height = prompt("Please enter the height");
var width = prompt("Please enter the width");
function calcArea(height, width) {
var area = height * width;
return area;
}
Here is the HTML code in which the JavaScript's outputs are coded.
<table>
<caption>Calculate the Area of a Rectangle</caption>
<tr><td class="makert">Height:</td>
<td class="makectr"> <script>document.write(height);</script></td></tr>
<tr><td class="makert">Width:</td>
<td class="makectr"> <script>document.write(width);</script></td></tr>
<tr><td class="makert">Area:</td>
<td class="makectr"> <script>document.write(area);</script></td></tr>
</table>
Share
Improve this question
asked Jan 29, 2019 at 20:31
codestudentcodestudent
732 silver badges8 bronze badges
1
- 1 Height and width are globally scoped. Area is a return value, not a global variable. – stealththeninja Commented Jan 29, 2019 at 20:32
4 Answers
Reset to default 5You should use your function:
<script>document.write(calcArea(height, width));</script>
The point of declaring a function is to use it later.
Its because area is a local variable to the calcArea
method and the document
object doesn't have access to it.
var area = 0;
var height = prompt("Please enter the height");
var width = prompt("Please enter the width");
Try this code instead since the above way isn't a practical way of coding:
var height = prompt("Enter Height: ");
var width = prompt("Enter Width: ");
(function calcArea() {
var area = +height * +width;
document.getElementsByClassName('makectr')[0].innerHTML = height;
document.getElementsByClassName('makert')[1].innerHTML = width;
document.getElementsByClassName('makectr')[2].innerHTML = area;
})();
<table>
<caption>Calculate the Area of a Rectangle</caption>
<tr>
<td class="makert">Height: </td>
<td class="makectr"></td>
</tr>
<tr>
<td class="makert">Width: </td>
<td class="makectr"></td>
</tr>
<tr>
<td class="makert">Area: </td>
<td class="makectr"></td>
</tr>
</table>
The area
variable is declared inside the function, so it does not exist outside of the function's scope.
Try something like this
var area = "...";
function calcArea(height, width) {
area = height * width;
return area;
}
also your function is never called, so area is never actually given a value.
You can use following code to calc values and separate JS and HTML
var q= s=>document.querySelector(s);
function calcArea(height, width) {
var area = height * width;
q('.heigh').innerHTML=height;
q('.width').innerHTML=width;
q('.area').innerHTML=area;
return area;
}
function btnClick() {
var height = prompt("Please enter the height");
var width = prompt("Please enter the width");
calcArea(height, width);
q('table').style.display='block'
}
<table style="display:none">
<caption>Calculate the Area of a Rectangle</caption>
<tr><td class="makert">Height: </td><td class="heigh makectr"></td></tr>
<tr><td class="makert">Width: </td><td class="width makectr"></td></tr>
<tr><td class="makert">Area: </td><td class="area makectr"></td></tr>
</table>
<button onclick="btnClick()">Calc area</button>
本文标签: How can I print the return value of a JavaScript function in HTMLStack Overflow
版权声明:本文标题:How can I print the return value of a JavaScript function in HTML? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741906869a2404200.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论