I am trying to create calculator using JQuery, HTML and CSS.
Numbers should be displayed on screen when I click on number button.
When I click on ‘C’ – I need to clear screen to default value.
When I select one operator, then another, the second one should replace first one on the screen.
When I click on ‘=’ – I need to display result on screen.
This is my code, but it doesn’t work properly.
js:
$(function () {
var functionButtons = "*/-+";
$(".calc-btn").each(function () {
var $t = $(this);
$(this).click(function () {
if (functionButtons.indexOf($(".result").val().slice(-1)) > 0) {
$(".result").val(
$(".result")
.val()
.slice(0, $(".result").val().length - 1) + $(this).html()
);
}
if ($(".result").val() != "NaN" && $(".result").val() != "Infinity") {
if (+$t.val() >= 0 && +$t.val() <= 9) {
$(".result").val($(".result").val() + $(this).html());
}
}
});
});
$(".equal").on("click", function () {
exp = $(".result").val();
$(".result").val(eval(exp));
});
$(".clear").on("click", function () {
$(".result").val("");
});
});