Build a calculator -codecademy- adding functions to buttons


I am beginning learning how to code and tried to program a calculator through a course in codecademy.
The task:
We need to set up a single .click() listener that when the user clicks a #numbers a (Use .not() to exclude the #clear and #clearall buttons), will take the .text() of the button, append that to number, set the .text() of totaldiv to number, and finally call testNumLength, passing in number as the parameter.

The interfase alsways saying that my .click() isn’t correctly defined and I don’t know how to fix my Code.
You can see my full code for html, jQuery, and javascript here


please also include exercise url, so we can run your code versus codecademy expectations of the code


here is the exercise url:


this is the bit of code we are interessted in:

    var number = "";
    var newnumber = "";
    var operator = "";
    var totaldiv = $("#total");
    $("#numbers a").not("#clear,#clearall").click(function(){
        $("#numbers").html("number"); {

i don’t even know where to start, there are several flaws within your code.

lets start here:

$("#numbers").html("number"); {

i am unsure why you use curly brackets, {}, within this lines, they are not needed.

from the instructions:

Takes the .html() of the number button and appends that to number

to retrieve a value with .html(), it shouldn’t have any arguments, why do you give it an argument of "number"?

We only want to retrieve the value of the element clicked, for which we can use $(this) to select the element, so to get the html value of the current element you get $(this).html()

the retrieved value should then be appended to number variable, this code is currently completely missing

from the instructions:

Sets the .text() of totaldiv to number

both totaldiv and number are variables, but looking at your code:


i don’t see you using variables at all


This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.