# Weird Issue with JS calculator

**URL:** <https://forum.freecodecamp.org/t/weird-issue-with-js-calculator/144119>\
**Category:** General\
**Created:** [August 27, 2017, 11:46am UTC](https://forum.freecodecamp.org/t/weird-issue-with-js-calculator/144119 "2017-08-27T11:46:43Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![heenslice](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/heenslice/32/103561_2.png) [@heenslice](https://forum.freecodecamp.org/u/heenslice)\
**Post date:** [August 27, 2017, 11:46am UTC](https://forum.freecodecamp.org/t/weird-issue-with-js-calculator/144119/1 "2017-08-27T11:46:43Z")

</div>

Hi everyone,

I started working on the JS calculator today and I am running into a weird issue.

you can find the codepen here:

[https://codepen.io/heenslice/pen/YxOdaM?editors=0010](https://codepen.io/heenslice/pen/YxOdaM?editors=0010)

I am currently attempting to to console.log the correct answers before displaying results to the screen.

My current issue is regarding this section.

```
// display numbers (first number)
$('.number').click(function(){
  var $buttonValue = $(this).text();
  totalString1 = totalString1 + $buttonValue;
  console.log(totalString1);
  return totalString1;
});

// apply operator
$('.operator').click(function(){
  $('.btn').addClass('number2');
  $('.btn').removeClass('number');
  if($(this).text() === '+'){
    console.log('add');
  }
});

// display numbers (second number)
$('.number2').click(function(){
  console.log('2nd function');
  var $buttonValue = $(this).text();
  totalString2 = totalString2 + $buttonValue;
  console.log(totalString2);
  return totalString2;
});

```

My logic is that after you create the first string and click on ‘+’. It will remove the number class. and add the number2 class. So the next time you click a number we will start creating the second string.

However, even after removing the number class. When I click a number it still fires the first function. Am I missing something really obvious?

Any help/advice would be much appreciated

---

<div class="post-metadata">

**Author:** ![heenslice](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/heenslice/32/103561_2.png) [@heenslice](https://forum.freecodecamp.org/u/heenslice)\
**Post date:** [August 27, 2017, 9:42pm UTC](https://forum.freecodecamp.org/t/weird-issue-with-js-calculator/144119/3 "2017-08-27T21:42:03Z")

</div>

Yeah, I managed to work around the above issue by using the following:

```auto
// display numbers (first number)
$('.number').click(function(){
  var $buttonValue = $(this).text();
  if($('.btn').hasClass('number')){
    totalString1 = totalString1 + $buttonValue;
    console.log('string1', totalString1);
    $('.display').text(totalString1);
    return totalString1;
  } else if($('.btn').hasClass('number2')){
    totalString2 = totalString2 + $buttonValue;
    console.log('string2', totalString2);
    $('.display').text(totalString2);
    return totalString2;
  }
});

```

This ensured the second function would fire after clicking an operator button. I’m still not sure why the original code didn’t work

---

<div class="post-metadata">

**Author:** ![heenslice](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/heenslice/32/103561_2.png) [@heenslice](https://forum.freecodecamp.org/u/heenslice)\
**Post date:** [August 27, 2017, 9:45pm UTC](https://forum.freecodecamp.org/t/weird-issue-with-js-calculator/144119/5 "2017-08-27T21:45:55Z")

</div>

Fair enough. Thanks anyway!

---

<div class="post-metadata">

**Author:** ![ILM](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/ilm/32/363239_2.png) [@ILM](https://forum.freecodecamp.org/u/ILM)\
**Post date:** [January 17, 2021, 8:41pm UTC](https://forum.freecodecamp.org/t/weird-issue-with-js-calculator/144119/6 "2021-01-17T20:41:15Z")

</div>

Closed on January 17, 2021, 8:41pm UTC


