Trying to make a calculator. It works as follows:
When you click on a number, it fits into the variable. When you click on the sign (+, -, *, /), they are added to the same variable as the digit. The result is for example:
var x = '18 * 2 + 3 '
I want to make it so that when you click on the equal sign, the equation located in the variable x has been solved, as for example, if it were just:
var x = 18 * 2 + 3.
console.log (x);
I tried the Number function and realized that I was doing something wrong.
Can you please tell me in what ways you can solve the problem and how can you automatically increase the height of the diva ('.example') when a certain number of characters are reached?
$(function() { var numbers = 0; var example = 0; // Пот нажатии на кнопку с цифро, он появляется в нижнем "табло" калькулятора $('.numBtn').click(function() { if (numbers.length >= 12) { return; } else { if (numbers == 0) { numbers = this.value; } else { numbers += this.value; } } $('.calcIn').text(numbers); }) // При нажатии на знак +, - , * или /, цифра и нажатый знак урованения добавляются в переменную example. $('.funcBtn').click(function(){ example += ($('.calcIn').text()) $('.desk').text(example+=this.value); numbers = 0 $('.calcIn').text(numbers) }) /*При нажатии на кнопку равно, результат примера находящегося в качестве строки в переменной example должен появиться в качестве текста в классе calcIn */ $('.equalBtn').click(function(){ // // $('.calcIn').text(result); }) }); <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.2.3/jquery.min.js"></script> <div class="simpleCalc"> <div class="scoreboard"> <div class="desk"> 0</div> <div class="calcIn" id="text">0</div> </div> <table> <input type="button" class="button bigButtonC cButton" value="C"> <input type="button" class="button topBtn CEbtn" value="CE"> <input type="button" class="button topBtn funcBtn" value="x2"> <input type="button" class="button topBtn funcBtn" value="del"> <input type="button" class="button topBtn funcBtn" value="÷"> <input type="button" class="button secLineBtn numBtn" value="7"> <input type="button" class="button secLineBtn numBtn" value="8"> <input type="button" class="button secLineBtn numBtn" value="9"> <input type="button" class="button secLineBtn funcBtn" value="×"> <br> <input type="button" class="button thirdLineBtn funcBtn" value="%"> <input type="button" class="button thirdLineBtn numBtn" value="4"> <input type="button" class="button thirdLineBtn numBtn" value="5"> <input type="button" class="button thirdLineBtn numBtn" value="6"> <input type="button" class="button thirdLineBtn funcBtn" value="−"> <input type="button" class="button fourthLineBtn funcBtn" value="√"> <input type="button" class="button fourthLineBtn numBtn" value="1"> <input type="button" class="button fourthLineBtn numBtn" value="2"> <input type="button" class="button fourthLineBtn numBtn" value="3"> <input type="button" class="button bigButtonPlus funcBtn" value="+"> <input type="button" class="button bottomLineBtn funcBtn" value="±"> <input type="button" class="button bottomLineBtn commaBtn" value=","> <input type="button" class="button bottomLineBtn numBtn" value="0"> <input type="button" class="button bottomLineBtn equalBtn" value="="> </table> </div>
eval, but - eval is evil . It is better to have a pool, where to throw the data of the form:[{type: 'number', value: 2}, {type: 'sign', value: '+'}, {type: 'number', value: 2}]. And by the button "Decide" to go through the array by the executer. - user207618