gpt4 book ai didi

javascript - 构建一个基本计算器

转载 作者:行者123 更新时间:2023-11-28 06:21:10 25 4
gpt4 key购买 nike

我目前正在学习如何编码,但在使用 jQuery 时遇到了一些困难。我尝试在我正在做的类(class)中将这个计算器作为一个项目来完成,但我遇到了一个障碍。我不知道如何修复我的代码:一个问题是操作不能正常工作,数字也不能正常工作。我尝试按照教程进行操作,并决定使用不同的按钮来进行操作,但我没有得到预期的结果。数字应显示在总计部分,并且运算符应给出数学结果。

您可以在此处查看我的 html、jQuery 和 javascript 的完整代码 http://codepen.io/IrvingG/pen/adRrvW

$(document).ready(function() {
var testNumLength = function(number) {
if (number.length > 9) {
totaldiv.text(number.substr(number.length - 9, 9));
if (number.length > 15) {
number = "";
totaldiv.text("Err");
}
}
};
var number = "";
var newnumber = "";
var operator = "";
var totaldiv = $("#results");
totaldiv.text("0");

$(".numbers button").not("#operators, #equals, #clear").click(function() {
number += this.html();
totaldiv.text(number);
testNumLength.text(number);
});
$("#operators").click(function() {
operator += this.html();
newnumber = number;
number = "";
totaldiv.text("0");
});

$("#clear, #clear1").click(function() {
number = "";
totaldiv.text("0");
if ($(this).attr("id") === "#clear") {
newnumber = ";"
}
});

$("#equals").click(function(){
if (operator === "+"){
number = (parseInt(number, 10) + parseInt(number, 10).toString(10));
} else if (operator === "-"){
number = (parseInt(number, 10) - parseInt(number, 10).toString(10));
} else if (operator === "/"){
number = (parseInt(number, 10) / parseInt(number, 10).toString(10));
} else if (operator === "*"){
number = (parseInt(number, 10) * parseInt(number, 10).toString(10));
}

totaldiv.text(number);
testNumLength(number);
number="";
newnumber="";
});

});

最佳答案

这里是代码Calculator

$(document).ready(function() {
var testNumLength = function(number) {
if (number.length > 9) {
totaldiv.text(number.substr(number.length - 9, 9));
if (number.length > 15) {
number = "";
totaldiv.text("Err");
}
}
};
var result;
var number = "";
var newnumber = "";
var operator = "";
var totaldiv = $("#results");
totaldiv.text("0");

$(".numbers button").not(".operator,#equals ,#clear").click(function() {
number += $(this).html();

totaldiv.text(number);
testNumLength(number);

});
$(".operator").click(function() {
operator = $(this).html();

newnumber = number;
number = "";
totaldiv.text("0");

});

$("#clear, #clear1").click(function() {
number = "";
totaldiv.text("0");
if ($(this).attr("id") === "#clear") {
newnumber = ";";
}
});

$("#equals").click(function(){
if (operator === "+"){
result=parseInt(newnumber, 10) + parseInt(number, 10);
number = result.toString(10);

} else if (operator === "-"){
result=parseInt(newnumber, 10) - parseInt(number, 10);

number = result.toString(10);
} else if (operator === "/"){
result=parseInt(newnumber, 10) / parseInt(number, 10);
number = result.toString(10);
} else if (operator === "*"){
result=parseInt(newnumber, 10) * parseInt(number, 10);
number = result.toString(10);
}

totaldiv.text(number);
testNumLength(number);
number="";
newnumber="";
});
});

关于javascript - 构建一个基本计算器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35517505/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com