Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/380.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 正在尝试使用JS构建计算器_Javascript_Html_Css_Calculator - Fatal编程技术网

Javascript 正在尝试使用JS构建计算器

Javascript 正在尝试使用JS构建计算器,javascript,html,css,calculator,Javascript,Html,Css,Calculator,刚刚完成了一段代码,根据YT教程构建了一个计算器。这个计算器看起来很好,但我不能用。我不明白视频里的每件事都是我一字不差地做的。有人能帮我吗?这段视频我看了好几次,但我就是无法让它正常工作,也不知道为什么。我猜appendNumber变量有问题。但我不确定他是否还是JS的新手 类计算器{ 构造函数(PreviousOperationTextElement、CurrentOperationTextElement){ this.PreviousOperationTextElement=Previo

刚刚完成了一段代码,根据YT教程构建了一个计算器。这个计算器看起来很好,但我不能用。我不明白视频里的每件事都是我一字不差地做的。有人能帮我吗?这段视频我看了好几次,但我就是无法让它正常工作,也不知道为什么。我猜appendNumber变量有问题。但我不确定他是否还是JS的新手

类计算器{
构造函数(PreviousOperationTextElement、CurrentOperationTextElement){
this.PreviousOperationTextElement=PreviousOperationTextElement
this.currentOperatureTextElement=currentOperatureTextElement
这个
}
清除(){
this.currentOperator=“”
this.previous操作数=“”
this.operation=未定义
}
删除(){
this.currentOperator=this.currentOperator.toString().slice(0,-1)
}
附录编号(编号){
if(number=='..&&this.currentOperator.includes('.')返回
this.currentOperator=this.currentOperator.toString()+number.toString()
}
选择操作(操作){
如果(this.currentOperator=='')返回
if(this.previousOperator!=''){
这个是compute()
}
此操作=操作
this.previous操作数=this.currentOperator
this.currentOperator=“”
}
计算(){
让计算
const prev=parseFloat(this.previous操作数)
const current=parseFloat(此.currentOperator)
如果(isNaN(上一个)| isNaN(当前))返回
开关(此操作){
格“+”:
计算=上一次+当前
打破
案例'-':
计算=上一次-当前
打破
案例“*”:
计算=上一次*当前
打破
案例“÷”:
计算=上一次/当前
打破
违约:
返回
}
this.currentOperator=计算
this.operation=未定义
this.previous操作数=“”
}
getDisplayNumber(数字){
const stringNumber=number.toString()
const interDigits=parseFloat(stringNumber.split('.')[0])
常量小数位数=stringNumber.split('.')[1]
让我们展开
if(isNaN(交指)){
整数显示=“”
}否则{
integerDisplay integerDigits.tpLocaleString('en'{
最大分数位数:0})
if(小数位数!=null){
返回“$(整数显示)。$(小数位数)`
}否则{
返回显示
}
}
updateDisplay(){
this.CurrentOperationTextElement.innerText=
此.getDisplayNumber(此.CurrentOperator)
if(this.operation!=null){
this.PreviousOperationsTextElement.innerText=
$(this.getDisplayNumber(this.previousOperator)$(this.operation)
}否则{
this.PreviousOperationTextElement=“”
}
}
const numberButtons=document.querySelectorAll(“[data number]”)
const operationButtons=document.querySelectorAll(“[data operation]”)
const equalsButton=document.querySelector(“[data equals]”)
const deleteButton=document.querySelector(“[data delete]”)
const allClearButton=document.querySelector(“[data all clear]”)
const previousOperationTextElement=document.querySelector(“[data previous Operator]”)
const currentOperatureTextElement=document.querySelector(“[数据当前操作数]”)
常量计算器=新的计算器(PreviousOperationsTextElement、CurrentOperationsTextElement)
numberButtons.forEach(按钮=>{
按钮。addEventListener('单击',()=>{
计算器.appendNumber(按钮.innerText)
calculator.updateDisplay()
})
})
操作按钮。forEach(按钮=>{
按钮。addEventListener('单击',()=>{
计算器.chooseOperation(按钮.innerText)
calculator.updateDisplay()
})
})
equalsButton.addEventListener('单击,按钮=>{
计算器
calculator.updateDisplay()
})
allClearButton.addEventListener('单击,按钮=>{
计算器清除()
calculator.updateDisplay()
})
deleteButton.addEventListener('单击,按钮=>{
计算器。删除()
calculator.updateDisplay()
})

新JS
*,*::之前,*::之后{
框大小:边框框;
字体大小:正常;
字体系列:高谭圆形,无衬线;
颜色:蓝色;
}
身体{
填充:0;
保证金:0;
背景:线性渐变(向右、蓝色、绿色);
}
.计算器网格{
显示:网格;
证明内容:中心;
对齐内容:居中对齐;
最小高度:100vh;
网格模板列:重复(4100px);
网格模板行:最小最大值(120px,自动)重复(5100px);
}
.计算器网格>按钮{
光标:指针;
字号:2rem;
边框:1px纯白;
大纲:无;
背景色:rgba(255、255、255、75);
}
.计算器网格>按钮:悬停{
背景色:rgba(255、255、255、.9);
}
.跨度2{
格构柱:跨度2;
}
.产出{
网格柱:1/-1;
背景色:rgba(0,0,75);
显示器:flex;
对齐项目:柔性端;
证明内容:周围的空间;
弯曲方向:立柱;
填充:10px;
分词;
单词break:打破一切;
}
.output.previous操作数{
颜色:rgba(255、255、255、75);
字体大小:1.5rem;
}
.output.当前操作数{
颜色:白色;
字体大小:2.5rem;
}
自动控制
德尔
+
1.
2.
3.
*
4.
5.
6.
÷ 
7.
8.
9
-
.
0