Hello! I was doing one of my projects, as to begin with, consisting in HTML, CSS and JavaScript all together. I have done a lot of progress in 3 days but now I have faced a bug i canât seem to find the solution to. The relevant part of my code where the bug might be is the next:

**HTML part:**

```
<li><input type="button" class="button" value="4" onclick='numero(4)'></li>
<li><input type="button" class="button" value="5" onclick='numero(5)'></li>
<li><input id="comma" type="button" class="sbutton" value="," onclick='decimal_true()'></li>
<li><input type="button" class="button" value="+" onclick='operate("+")'></li>
<li id="output">0</li>
```

**JavaScript**

```
var operation = [0,' ', 0, 0];
var answer;
var decimal = false;
var decimal_true = function () {
decimal=true;
}
var decimal_two = false;
var decimal_two_true = function () {
decimal_two = true;
}
var operate = function(y) {
operation[2] = 0;
operation[1] = y;
operation.reverse();
document.getElementById("output").innerHTML = operation[0];
decimal=false;
document.getElementById("comma").removeEventListener("click", decimal_true);
document.getElementById("comma").addEventListener("click", decimal_two_true);
};
```

**JavaScript part returning NaN:**

```
if(decimal) {
operation[2]= operation[2].toString();
var l = operation[2].length;
var y = Math.pow(10,l);
operation[0]-=(operation[2]/y);
operation[2]*=10;
operation[2]+=x;
operation[2]= operation[2].toString();
l= operation[2].length;
y = Math.pow(10, l);
operation[2] = parseInt(operation[2]);
operation[0]+=(operation[2]/y);
document.getElementById("output").innerHTML = operation[0];
} else if (decimal_two) {
operation[1]= operation[1].toString();
var l = operation[1].length;
var y = Math.pow(10,l);
operation[0]-=(operation[1]/y);
operation[1]*=10;
operation[1]+=x;
operation[1]= operation[1].toString();
l= operation[1].length;
y = Math.pow(10, l);
operation[1] = parseInt(operation[1]);
operation[0]+=(operation[1]/y);
document.getElementById("output").innerHTML = operation[0];
} else {
operation[0]*=10;
document.getElementById("output").innerHTML = operation[0]+=x;
}
};
```

**Explanation on Code**

The code is composed of 2 parts. The HTML has buttons which when pressed they run a function. The JavaScript is the one doing the calculations. I copied some relevant lines where the problem might be.

Decimal_two and Decimal are activated when I press the Button with id=âcommaâ, one is used when for the first number, the other for the second. I had to separe them as when I use .reverse(), the array position which stores the decimals move from 2 to 1. I wonât explain more unless somebody asks for it. Letâs move to the bug.

**Getting NaN**

I can easily assign decimal positions to the first numbers and do things like the next:

- 1 + 1
- 1.2 + 3
- 1.2345 + 6

But when it comes to assigning decimal digits to the second number, i get NaN. The following is the steps I do:

Any number +âŚ / Press 5 / Press the comma button / Press a number

As soon as I press the number which will go into the decimal place, I get NaN on the output. I have no idea why this happens, I have read it many times, but I keep getting NaN and canât find the mistake.

Let me say Thousands of Thanks to anyone who solves this. If you need any extra part of the code, let me know, I just posted this because I think itâs the most relevant part but I may have missed something.

Thx