0

Here's my code:

<script type="text/javascript" language="javascript">
function calc(){
    var a = prompt("Enter a number");
    var b = prompt("Enter another number");
    var x = +a;
    var y = +b;
    alert("The answer is " + (x+y));
}
function rect(type){
    var a = prompt("Enter length of rectangle");
    var b = prompt("Enter width of rectangle");
    var x = +a;
    var b = +b;
    if(type=="area"){
        alert("The area is " + (x*y);
    }
    else if(type=="perimeter"){
        alert("The perimeter is " + ( (2*x)+(2*y) );
    }
}
</script>

<head>
    <title>JavaScript Calculator</title>
</head>

<body>
    <button onclick="calc()">Calculator</button>
    <button onclick="rect('area')">Area of Rectangle</button>
    <button onclick="rect('perimeter')">Perimeter of Rectangle</button>
</body>

When I comment out the rect function, my code runs fine, it's just that the Calculator button is the only one that works. The other two buttons should be working fine with the rect function, but they aren't, and when I un-comment the rect function the Calculator button stops working as well. Why is this?

1
  • 1
    Make sure you check your error console before asking questions... Commented Dec 26, 2012 at 21:13

1 Answer 1

5

You're missing some parentheses

alert("The area is " + (x*y));
// and:
alert("The perimeter is " + ( (2*x)+(2*y) ));
Sign up to request clarification or add additional context in comments.

1 Comment

Nah, it's those little mistakes that are ones you look over.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.