What's wrong with my code?


#1
$(document).ready(function(){
	var testNumLength = function(number) {
        if (number.length > 9) {
            totaldiv.text(number.substr(number.length-9,9));
            if (number.length > 15) {
                number = "";
                totaldiv.text("Err");
            }
        } 
    };
	var number = "";
    var newnumber = "";
    var operator = "";
    var totaldiv = $("#total");
    totaldiv.text("0");
    $("#numbers a").not("#clear,#clearall").click(function(){
		number += $(this).text();
		totaldiv.text(number);
		testNumLength(number);
    });
    $("#operators a,#side a").not("#equals,#decimal").click(function(){
		operator = $(this).text();
		newnumber = number;
		number = "";
		totaldiv.text("0");
		if ($(this).attr("id") === $("#sqrt")){
		    operator.text($("#sqrt"));
		    testNumLength(number);
		    return "";//make a javascript calculator
		}
    });
    $("#decimal").click(function(){
        var numOfDecs = 0;
        var i;
        for (i = 0;i<number.length;i++){
            if (number[i] === "."){
                numOfDecs+=1;
            }
        }
        if(numOfDecs === 0){
            number = number+"."
        }
        totaldiv.text(number);
        testNumLength(number);
    });
    $("#clear,#clearall").click(function(){
		number = "";
		totaldiv.text("0");
		if ($(this).attr("id") === "clearall") {
			newnumber = "";
		}
    });
    //Add your last .click() here!
$("#equals").click(function(){
    	if (operator === "+"){
    		number = (parseInt(number, 10) + parseInt(newnumber,10)).toString(10);
    	} else if (operator === "-"){
    		number = (parseInt(newnumber, 10) - parseInt(number,10)).toString(10);
    	} else if (operator === "÷"){
    		number = (parseInt(newnumber, 10) / parseInt(number,10)).toString(10);
    	} else if (operator === "×"){
    		number = (parseInt(newnumber, 10) * parseInt(number,10)).toString(10);
    	} else if (operator === "sqrt"){
        number = Math.sqrt(number);
        (parseInt(number,10).toString(10))
        } else if (operator === "^"){
            newnumber = Math.pow(newnumber, number);
            (parseInt(newnumber,10)).toString(10);
    }
    	totaldiv.text(number);
    	testNumLength(number);
    	number = "";
    	newnumber = "";

It gives me this error: “Your #equals else if for #sqrt is not working!”
What’s wrong?


#2

Needs to be assigned. Same with,


#3

@mtf
I did what you said and this is what I did:
sqrt:

number = Math.sqrt(number);
number = (parseInt(number,10).toString(10));

power:

newnumber = Math.pow(newnumber, number);
number = (parseInt(newnumber,10).toString(10));

… and it still didn’t work and it said the same error.
Again, this is my full code:

$(document).ready(function(){
	var testNumLength = function(number) {
        if (number.length > 9) {
            totaldiv.text(number.substr(number.length-9,9));
            if (number.length > 15) {
                number = "";
                totaldiv.text("Err");
            }
        } 
    };
	var number = "";
    var newnumber = "";
    var operator = "";
    var totaldiv = $("#total");
    totaldiv.text("0");
    $("#numbers a").not("#clear,#clearall").click(function(){
		number += $(this).text();
		totaldiv.text(number);
		testNumLength(number);
    });
    $("#operators a,#side a").not("#equals,#decimal").click(function(){
		operator = $(this).text();
		newnumber = number;
		number = "";
		totaldiv.text("0");
		if ($(this).attr("id") === $("#sqrt")){
		    operator.text($("#sqrt"));
		    testNumLength(number);
		    return "";//make a javascript calculator
		}
    });
    $("#decimal").click(function(){
        var numOfDecs = 0;
        var i;
        for (i = 0;i<number.length;i++){
            if (number[i] === "."){
                numOfDecs+=1;
            }
        }
        if(numOfDecs === 0){
            number = number+"."
        }
        totaldiv.text(number);
        testNumLength(number);
    });
    $("#clear,#clearall").click(function(){
		number = "";
		totaldiv.text("0");
		if ($(this).attr("id") === "clearall") {
			newnumber = "";
		}
    });
    //Add your last .click() here!
    $("#equals").click(function(){
    	if (operator === "+"){
    		number = (parseInt(number, 10) + parseInt(newnumber,10)).toString(10);
    	} else if (operator === "-"){
    		number = (parseInt(newnumber, 10) - parseInt(number,10)).toString(10);
    	} else if (operator === "÷"){
    		number = (parseInt(newnumber, 10) / parseInt(number,10)).toString(10);
    	} else if (operator === "×"){
    		number = (parseInt(newnumber, 10) * parseInt(number,10)).toString(10);
    	} else if (operator === "√"){
    	    number = Math.sqrt(number);
    	    number = (parseInt(number,10).toString(10));
        } else if (operator === "^"){
            newnumber = Math.pow(newnumber, number);
            (parseInt(newnumber,10).toString(10));
    }
    	totaldiv.text(number);
    	testNumLength(number);
    	number = "";
    	newnumber = "";
    });
});

Can you help me??


#4

That one also needs to be assigned.

Is there some special reason to use parseInt()?

Please post a link to the project so we can test your code. Thanks.


#5

@mtf
here is the link:
calculator ||
And also what do you mean by assinged???


#6
a = value

value is assigned to a. In Python (and most programming languages) assignments are always right to left and always use = (assignment operator).


#7

@mtf
Thanks. Just what I thought. Did you get the link and find my problem?

Thanks,
@theorangemonkey001


#8
        else if (operator === "sqrt"){
            number = (parseInt(number, 10) ** 0.5);
    	}

as opposed to,

Use sqrt as the operator since that is the id of the button. Convert number to an integer in the Math.sqrt() argument.


#9

@mtf

does that work for power because it’s giving the same error.
here is the code:

$(document).ready(function(){
	var testNumLength = function(number) {
        if (number.length > 9) {
            totaldiv.text(number.substr(number.length-9,9));
            if (number.length > 15) {
                number = "";
                totaldiv.text("Err");
            }
        } 
    };
	var number = "";
    var newnumber = "";
    var operator = "";
    var totaldiv = $("#total");
    totaldiv.text("0");
    $("#numbers a").not("#clear,#clearall").click(function(){
		number += $(this).text();
		totaldiv.text(number);
		testNumLength(number);
    });
    $("#operators a,#side a").not("#equals,#decimal").click(function(){
		operator = $(this).text();
		newnumber = number;
		number = "";
		totaldiv.text("0");
		if ($(this).attr("id") === $("#sqrt")){
		    operator.text($("#sqrt"));
		    testNumLength(number);
		    return "";//make a javascript calculator
		}
    });
    $("#decimal").click(function(){
        var numOfDecs = 0;
        var i;
        for (i = 0;i<number.length;i++){
            if (number[i] === "."){
                numOfDecs+=1;
            }
        }
        if(numOfDecs === 0){
            number = number+"."
        }
        totaldiv.text(number);
        testNumLength(number);
    });
    $("#clear,#clearall").click(function(){
		number = "";
		totaldiv.text("0");
		if ($(this).attr("id") === "clearall") {
			newnumber = "";
		}
    });
    //Add your last .click() here!
    $("#equals").click(function(){
    	if (operator === "+"){
    		number = (parseInt(number, 10) + parseInt(newnumber,10)).toString(10);
    	} else if (operator === "-"){
    		number = (parseInt(newnumber, 10) - parseInt(number,10)).toString(10);
    	} else if (operator === "÷"){
    		number = (parseInt(newnumber, 10) / parseInt(number,10)).toString(10);
    	} else if (operator === "×"){
    		number = (parseInt(newnumber, 10) * parseInt(number,10)).toString(10);
    	} else if (operator === "sqrt"){
            number = (parseInt(number, 10) ** 0.5);
        } else if (operator === "^"){
            newnumber = Math.pow(newnumber, number);
            (parseInt(newnumber,10).toString(10));
    }
    	totaldiv.text(number);
    	testNumLength(number);
    	number = "";
    	newnumber = "";
    });
});

#10

Anyone including @mtf,
Please, can you answer. I am in a hurry because I have a deadline by 8:25 am today.

Sincerely,
@theorangemonkey001


#11
$("#equals").click(function(){
    	if (operator === "+"){
    		number = (parseInt(number, 10) + parseInt(newnumber,10)).toString(10);
    	} else if (operator === "-"){
    		number = (parseInt(newnumber, 10) - parseInt(number,10)).toString(10);
    	} else if (operator === "÷"){
    		number = (parseInt(newnumber, 10) / parseInt(number,10)).toString(10);
    	} else if (operator === "×"){
    		number = (parseInt(newnumber, 10) * parseInt(number,10)).toString(10);
    	} else if (operator === "√"){
    	    number = Math.sqrt(number);
    	    number = parseFloat(number,10).toString(10);
    	} else if(operator === "^"){
    	    number = Math.pow(newnumber,number);
    	    number = parseFloat(number,10).toString(10);
    	}
    	totaldiv.text(number);
    	testNumLength(number);
    	number = "";
    	newnumber = "";
    });
});

Can anyone help me with this problem?
My editor,Codecademy gives me an error saying,

here is the rest of my code if you want to run it
javascript:

$(document).ready(function(){
	var testNumLength = function(number) {
        if (number.length > 9) {
            totaldiv.text(number.substr(number.length-9,9));
            if (number.length > 15) {
                number = "";
                totaldiv.text("Err");
            }
        } 
    };
	var number = "";
    var newnumber = "";
    var operator = "";
    var totaldiv = $("#total");
    totaldiv.text("0");
    $("#numbers a").not("#clear,#clearall").click(function(){
		number += $(this).text();
		totaldiv.text(number);
		testNumLength(number);
    });
    $("#operators a,#side a").not("#equals,#decimal").click(function(){
		operator = $(this).text();
		newnumber = number;
		number = "";
		totaldiv.text("0");
		if ($(this).attr("id") === $("#sqrt")){
		    operator.text($("#sqrt"));
		    testNumLength(number);
		    return "";//make a javascript calculator
		}
    });
    $("#decimal").click(function(){
        var numOfDecs = 0;
        var i;
        for (i = 0;i<number.length;i++){
            if (number[i] === "."){
                numOfDecs+=1;
            }
        }
        if(numOfDecs === 0){
            number = number+"."
        }
        totaldiv.text(number);
        testNumLength(number);
    });
    $("#clear,#clearall").click(function(){
		number = "";
		totaldiv.text("0");
		if ($(this).attr("id") === "clearall") {
			newnumber = "";
		}
    });
    //Add your last .click() here!
$("#equals").click(function(){
    	if (operator === "+"){
    		number = (parseInt(number, 10) + parseInt(newnumber,10)).toString(10);
    	} else if (operator === "-"){
    		number = (parseInt(newnumber, 10) - parseInt(number,10)).toString(10);
    	} else if (operator === "÷"){
    		number = (parseInt(newnumber, 10) / parseInt(number,10)).toString(10);
    	} else if (operator === "×"){
    		number = (parseInt(newnumber, 10) * parseInt(number,10)).toString(10);
    	} else if (operator === "sqrt"){
        number = Math.sqrt(number);
        (parseInt(number,10).toString(10))
        } else if (operator === "^"){
            newnumber = Math.pow(newnumber, number);
            (parseInt(newnumber,10)).toString(10);
    }
    	totaldiv.text(number);
    	testNumLength(number);
    	number = "";
    	newnumber = "";
  });
});

css:

body, div, a {
    padding:0;
    margin:0;
    font-family:"Trebuchet MS", Arial, Helvetica, sans-serif;
}

#calculator {
	background-color: #999;
	width: 310px;
	height: 460px;
	border: 3px black solid;
	margin: 10px auto;
	padding: 5px;
	-moz-border-radius: 10px;
	-webkit-border-radius: 10px;
	-khtml-border-radius: 10px;
	border-radius: 10px;
}

#calculator a {
	display: block;
    float: left;
    cursor: pointer;
    text-decoration: none;
    text-align: center;
    background: -moz-linear-gradient(top, #EDEDED, #DCDCDC);
    background: -webkit-gradient(linear, left top, left bottom, from(#EDEDED), to(#DCDCDC));
    filter: progid:DXImageTransform.Microsoft.gradient(startColorstr='#EDEDED', endColorstr='#DCDCDC');
	-moz-border-radius: 10px;
	-webkit-border-radius: 10px;
	-khtml-border-radius: 10px;
    border-radius: 10px;
    color: black;
}

#numbers, #operators {
	margin: auto;	
}

#operators a {
	width: 46px;
	height: 45px;
	font-size: 39px;
	padding: 2px;
	margin: 5px 6px;
}

#numbers a {
	width: 50px;
	height: 50px;
	font-size: 32px;
	padding: 10px;
	margin: 5px;
}

#total {
    height: 70px;
    width: 300px;
    margin:auto;
    background-color:white;
    margin-bottom: 5px;
    text-align: right;
    font-size: 60px;
    padding: 0px 5px;
}

#side {
	width: 49px;
	float: right;
}

#side a {
	width: 40px;
	height: 40px;
	float: right;
	font-size: 32px;
	padding: 10px;
	margin: 5px;
}

a#equals {
	padding-top: 50px;
	height: 100px;
}

html:

<!DOCTYPE html>
<html>
    <head>
        <title>Calculator</title>
        <link rel="stylesheet" href="style.css" />
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js"></script>
        <script src="script.js"></script>
    </head>
    <body>
        <div id="calculator">
            <div id="total">
            </div>
            <div id="operators">
                <a>+</a>
                <a>-</a>
                <a>&divide;</a>
                <a>&times;</a>
            </div>
            <div id="side">
                <a id="sq">^</a>
                <a id="sqrt">√</a>
                <a id="decimal">.</a>
                <a id="equals">=</a>
            </div>
            <div id="numbers">
                <a>1</a>
                <a>2</a>
                <a>3</a>
                <a>4</a>
                <a>5</a>
                <a>6</a>
                <a>7</a>
                <a>8</a>
                <a>9</a>
                <a id="clear">C</a>
                <a>0</a>
                <a id="clearall">AC</a>
            </div>
        </div>
    </body>
</html>