# Thread: Javascript results in text boxes

1. New Comer (less than 5 posts)
Join Date
Aug 2012
Posts
1
Thanks
0
Thanked 0 Times in 0 Posts

## Javascript results in text boxes

hello there,
I have recently learned JS and i decided to start making a calculator for my webpage. the calculations went fine, but it's my first time trying to display the results in a text box in a form and i still haven't been able to figure it out. I get the number(s) and operator from a series of 3 prompts which activate when you click a button, and then i included a text box to display the results on mouse over (but i would be just as satisfied if i could just normally display it) , but nothing happens! Its probably from some error in the scripting, but i don't know what! Here is the script:

<script type="text/javascript">
function calculator(){
var number = prompt("Enter the 1st number");
var operator = prompt("Enter the operator (+,-,*,/,^2,^3,^,sqrt(),abs(),1/x,!,sin(),cos(),tan(),cot(),sec(),csc(),ln())");
var number2 = prompt("Enter the 2nd number");

return parseInt(number) + parseInt(number2);
}

function factorial(number) {
if ((number == 0) || (number == 1))
return 1
else {
answer = (number * factorial(number-1) )
}
}

if (operator == "-"){
}else if (operator == "*"){
}else if (operator == "/"){
}else if (operator == "^2"){
}else if (operator == "+"){
}else if (operator == "^3"){
}else if (operator == "^"){
}else if (operator == "sqrt()" && number >= 0){
}else if (operator == "sqrt()" && number < 0){
}else if (operator == "abs()"){
}else if (operator == "1/x"){
}else if (operator == "!"){
}else if (operator == "sin()"){
}else if (operator == "cos()"){
}else if (operator == "tan()"){
}else if (operator == "cot()"){
}else if (operator == "sec()"){
}else if (operator == "csc()"){
}else if (operator == "ln()"){
}else{
alert("This calculator does not currently support operators other than addition, subtraction, multiplication, division, squaring, cubing, exponentation, square roots, absolute values, reciprocals, factorials, the 6 trig functions and natural logarithms.")
}

}

}
</script>

...(I know its long) And here's the corresponding relevent HTML:

<form id="form">
<input type="button" onclick="calculator()" value="Click to Start!" />
</form>

any help would be much appreciated and thanks for your time

2. Elite Coders
Join Date
Dec 2008
Location
Portsmouth, UK
Posts
1,889
Thanks
2
Thanked 441 Times in 435 Posts
Code:
```<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

<title></title>
<script type="text/javascript">

function calculator(){
document.getElementById('display').value = '';
var number = prompt("Enter the 1st number");
var operator = prompt("Enter the operator (+,-,*,/,^2,^3,^,sqrt(),abs(),1/x,!,sin(),cos(),tan(),cot(),sec(),csc(),ln())");
var number2 = prompt("Enter the 2nd number");

if (operator == "-"){
}
else if (operator == "*"){
}
else if (operator == "/"){
}
else if (operator == "^2"){
}
else if (operator == "+"){
}
else if (operator == "^3"){
}
else if (operator == "^"){
}
else if (operator == "sqrt()" && number >= 0){
}
else if (operator == "sqrt()" && number < 0){
}
else if (operator == "abs()"){
}
else if (operator == "1/x"){
}
else if (operator == "!"){
}
else if (operator == "sin()"){
}
else if (operator == "cos()"){
}
else if (operator == "tan()"){
}
else if (operator == "cot()"){
}
else if (operator == "sec()"){
}
else if (operator == "csc()"){
}
else if (operator == "ln()"){
}
else{
alert("This calculator does not currently support operators other than addition, subtraction, multiplication, division, squaring, cubing, exponentation, square roots, absolute values, reciprocals, factorials, the 6 trig functions and natural logarithms.")
}
}

return parseInt(number) + parseInt(number2);
}

function factorial(number) {
if ((number == 0) || (number == 1))
return 1
else {
answer = (number * factorial(number-1) )
}
}

function output() {
}

<body>
<form id="form">
<input type="button" onclick="calculator()" value="Click to Start!" />
<input type="text" id="display" onmouseover="output()" />
</form>

</body>

</html>```

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•