Friday, May 17, 2024
66
rated 0 times [  73] [ 7]  / answers: 1 / hits: 15604  / 9 Years ago, fri, december 18, 2015, 12:00:00

I'm trying to create a simple javascript application that will ask the user to enter the radius of a circle, and in return will display the circumference and area in sentence form after the user hits Calculate. Right now, when the user enters a number and hits calculate, nothing happens.



The JavaScript is included in the HTML document like this:



<!DOCTYPE html>
<html>
<head>
<meta charset = utf-8>
<title>Create a Circle</title>
<h1 style=text-align:center>Create a Circle!</h1>
<br>
<div style=text-align:center>
Enter the radius for your circle:
<input type=text id=txtRadius size=10 />
<br>
<input type=button value=Calculate onclick=CalculateArea()/>

<script>

function print() {
var p =
document.createElement(p),
text = Array.prototype.join.call(arguments,,);
p.textContent = text;
document.getElementById(console).appendChild(p);
return text;
}

function CalculateCircumference() {
var radius =
parseInt(document.getElementById('txtRadius').value);//String to Integer

if (0 < radius)
print(The circumference of the circle is + (radius * 2 * Math.PI);
else
print(Error - radius must be a whole number greater than 0.);
return false;
}

function CalculateArea() {
var radius =
parseInt(document.getElementById('txtRadius').value); //String to Integer

if (0 < radius)
print(The area of the circle is + (radius * radius * Math.PI);
else
print(Error - radius must be a whole number greater than 0.);
return false;
}
</script>
</head>
<body>
</body>
</html>


I'm new to the print method so I tried changing all prints to alert, but this didn't do anything. Thank you for your help. Link to JSFiddle: https://jsfiddle.net/HappyHands31/xzsf8ca4/2/


More From » if-statement

 Answers
35

You have more issues with your code however this will answer your question



The reason nothing happens is because you are missing a ) and is makeing the function invalid.



change this: print(The area of the circle is + (radius * radius * Math.PI);



to this: print(The area of the circle is + (radius * radius * Math.PI));



See fiddle https://jsfiddle.net/xzsf8ca4/4/


[#64017] Wednesday, December 16, 2015, 9 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
donaldcristianl

Total Points: 114
Total Questions: 95
Total Answers: 110

Location: Bonaire
Member since Sat, May 27, 2023
1 Year ago
;