Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7685573
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T19:16:51+00:00 2026-05-31T19:16:51+00:00

I have been stuck on this and don’t really know how to go about

  • 0

I have been stuck on this and don’t really know how to go about fixing it myself. I’ve tried using firebug and getting parts of it to work, but it’s just so frustrating and time consuming for such a small script. Perhaps I’m using the switch statement wrong…

Essentially I’m making a very simple calculator using an Object for the first time…
Any little bit will help–any push in the right direction 🙂

Thanks so much for reading!

The HTML file is this:

    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>

    <title>Homework 8</title>
        <link href="css/main.css" rel="stylesheet" type="text/css"/>
</head>

<body>
    <div id="calc">
        <p><label for="num1">First Number:<input type="text" id="num1" class="txt" /></label></p>
        <p><label for="num2">Second Number: <input type="text" id="num2" class="txt"/></label></p>
        <p><label for ="add"><input type="radio" name="operation" id="add" value="add"/>Add</label> 
        <label for ="sub"><input type="radio" name="operation" id="sub" value="sub"/>Subtract</label> 
        <label for ="mult"><input type="radio" name="operation" id="mult" value="mult"/>Multiply</label> 
        <label for ="div"><input type="radio" name="operation" id="div" value="div"/>Divide</label></p>
        <p><input type="button" id="calculate" value="Do Calculation"/></p>
    </div>
    <div id="result"></div>
<script type="text/javascript" src="js/hw8.js"></script>
<script type="text/javascript">obj.init();</script>
</body>
</html>

The Javascript file is this short script:

var obj = {

add : document.getElementById("add"),
sub : document.getElementById("sub"),
mult : document.getElementById("mult"),
div : document.getElementById("div"),
num1 : document.getElementById("num1"),
num2 : document.getElementById("num2"),
result : document.getElementById("result"),
calculate : document.getElementById("calculate"),

    init : function(){

        obj.calculate.onclick = obj.resultArea;

    },

    resultArea : function(){

        var num1Value = parseFloat(num1.value);
        var num2Value = parseFloat(num2.value);

        if (!isNaN(obj.num1Value) = true || !isNaN(obj.num2Value) = true){
            alert("Please enter only numbers in the First Number and Second Number fields!");
            return;
        }

        switch(true){
            case obj.add.checked : var ans = (num1Value + num2Value); break;
            case obj.sub.checked : var ans = (num1Value - num2Value); break;
            case obj.mult.checked : var ans = (num1Value * num2Value); break;
            case obj.div.checked && num2Value != 0 : var ans = (num1Value / num2Value); break;
            case obj.div.checked && num2Value = 0 : alert("cannot divide by zero"); break;
        }


        var p = document.createElement("p");
        p.appendChild(document.createTextNode("The answer is" + ans));
        obj.result.appendChild(p);
    },

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T19:16:52+00:00Added an answer on May 31, 2026 at 7:16 pm
    if (!isNaN(obj.num1Value) = true || !isNaN(obj.num2Value) = true)
    

    Besides the the error due confusing the assignment operator (=) and the equality comparison operator (==) operator this
    is overly complex.

    If not obj.num1Value is not a number is true and not obj.num2alue is not a number is true do?

    • Say what?

    Consider this formulation:

    if ( isNaN(obj.num1Value) || isNaN(obj.num2Value) )
    

    Ah, if either obj.num1Value or obj.num2Value is not an number do…

    Also you may want to declare the ans and ´p´ variable once at the top of the resultArea function:

    resultArea : function(){
        var ans;
        var p;
    
        // ...
    
        switch(true){
            // Note that we removed var keyword! 
            case obj.add.checked : ans = (num1Value + num2Value); break;
                // ..
    

    Also if you use a line break for each case in the switch statement it becomes much easier to follow
    the control flow:

    switch(true){
            case obj.add.checked :  
                ans = (num1Value + num2Value); 
                break;
            case obj.sub.checked :  
                ans = (num1Value - num2Value); 
                break;
            case obj.mult.checked :  
                ans = (num1Value * num2Value); 
                break;
            case obj.div.checked && num2Value !== 0 : 
                ans = (num1Value / num2Value); 
                break;
            case obj.div.checked && num2Value == 0 : 
                alert("cannot divide by zero"); 
                break;
        }
    

    You are not you are not covering the scenario where you do not have an answer:

     if ( typeof ans !== 'string') {
        p.appendChild(document.createTextNode("The answer is forever blowing in the wind"));
     }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been stuck on this for a while. i have tried and I
i know this is basic but somehow i have been stuck here for some
i'm using css to make a webpage and i have been stuck on this
I've been stuck on this problem for a while now and have tried my
I have been stuck on this all afternoon, and even tried the following search:
I have been stuck with this problem for quite some time now and i
I have been stuck on this one for a while, I'm not an expert
I have been stuck on this for days, and was wondering if anyone had
I have been stuck on this silly if statement, whatever i do, I cannot
so i have been stuck with this issue for 3 weeks now and i

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.