简体   繁体   English

FreeCodeCamp Javascript 计算器测试套件 | 我不知道为什么我不能通过这个测试

[英]FreeCodeCamp Javascript calculator test suite | I don't know why I can't pass this test

So I'm doing that FCC javascript calculator test, and I'm just stuck.所以我正在做 FCC javascript 计算器测试,我只是卡住了。

my code seems to be working but test suite says I'm not getting half of it so I just wanted to ask if you guys can point me to a write direction.我的代码似乎在工作,但测试套件说我没有得到一半,所以我只想问你们是否可以指出我的写作方向。

Also I'm keep getting this msg:我也不断收到这个消息:

    Uncaught SyntaxError: Invalid left-hand side in assignment.

But I don't know what it's referring to.但我不知道它指的是什么。

const equalStyle = {
  height: 120,
  backgroundColor: "#995ba4",
  bottom:7,
  position:"absolute"
};
const clearStyle = {backgroundColor: "#b83146"};
const equationStyle = {backgroundColor: "#b2b2b2"};
const equation = /[/*+-]/;
const confirmEquation = /[/*+-]$/;
const secondConfirm =/\d[x/+‑*]‑$/;
const negative = /[-]/;
const dotTester = /[.]/;
const prevTester = /^[/*-+]/;
      

class Main extends React.Component{
  constructor(props){
    super(props);
    this.state={
      totalVal:"",
      testVal:"0",
      displayVal:"0",
      prevVal:""
    }
    this.clickedVal = this.clickedVal.bind(this);
    this.calDisplay = this.calDisplay.bind(this);
  }
  
 calDisplay(val){
    if(this.state.displayVal == "0"){
      if(this.state.totalVal == ""){
        this.setState({
        totalVal: val
        })
      }
    } 
    else{
      this.setState({totalVal:this.state.totalVal + val})
    }
  }
  
  
 clickedVal(e){
    let clickVal = e.target.value;
    if (clickVal=="x"){clickVal = clickVal.replace("x", "*")};
   
    if(this.state.totalVal.includes("=")){
      this.setState({
        totalVal:"",
        testVal:"0",
        displayVal:"0"
      })
    }
   
  setTimeout(()=>{
      if(this.state.testVal == "0"&&!isNaN(parseInt(clickVal))){
        this.setState({ displayVal:clickVal, testVal:clickVal}, this.calDisplay(clickVal))
      }
      
      else if (!isNaN(parseInt(clickVal))){
      this.setState({ displayVal: this.state.displayVal + clickVal,testVal:this.state.displayVal + clickVal},this.calDisplay(clickVal))
      }
      /*equation stuff*/
      else if(equation.test(clickVal)){
          if(confirmEquation.test(this.state.totalVal)){
              if(negative.test(clickVal)){
                this.calDisplay(clickVal)
                this.setState({displayVal: clickVal, testVal:"0"})
              }
           
              else if(secondConfirm.test(this.state.totalVal)){
                  this.setState({
                      displayVal: clickVal, testVal:"0", totalVal: this.state.totalVal.slice(0, this.state.totalVal.length-2)
                    });
              setTimeout(()=>this.calDisplay(clickVal))
              }
            
              else{
                  this.setState({
                      displayVal: clickVal, testVal:"0", totalVal: this.state.totalVal.slice(0, this.state.totalVal.length-1)
                  });
              setTimeout(()=>this.calDisplay(clickVal))
              }
          }  
      
        else if (typeof(this.state.prevVal) == "number"  && this.state.displayVal =="0"){
            this.setState({displayVal:clickVal, testVal:"0"})
            this.calDisplay(this.state.prevVal+clickVal)
        }
        
        else{
            this.setState({displayVal:clickVal, testVal:"0"})
            this.calDisplay(clickVal);
        }
    }
      
      else if(clickVal == "."){
        if(confirmEquation.test(this.state.totalVal)){
          this.calDisplay("0.")
          this.setState({ displayVal: "0.", testVal: "0"})
      }
      else if (dotTester.test(this.state.displayVal)){
          this.setState({displayVal: this.state.displayVal ,testVal:this.state.displayVal})
      }
      else{
          this.calDisplay(clickVal);
          this.setState({displayVal: this.state.displayVal + clickVal,testVal:this.state.displayVal + clickVal})
      }
    }
      
      else if (clickVal == "="){
         
        let result = Math.round(1000000000000 * eval(this.state.totalVal)) / 1000000000000;
        this.setState({
          displayVal: result, testVal:"0", prevVal:result
        })
        this.calDisplay("="+result)
      }
      
      else if (clickVal == "AC"){
        this.setState({
          totalVal:"",
          testVal:"0",
          displayVal:"0",
          prevVal:""
      })
    }
   })    
  }
  
  render(){

    return(
      <div className = "calculatorBody" >
        <OutputDisplay formula = {this.state.totalVal}/>
        <InputDisplay currentValue = {this.state.displayVal} />
        <Buttons onClick = {this.clickedVal}/>
      </div>
    )
  }
}

https://codepen.io/arthur-lee945/pen/NWaebeV?editors=0111 https://codepen.io/arthur-lee945/pen/NWaebeV?editors=0111

This is my codepen for it.这是我的代码笔。

The error occurs when 'this.state.totalVal' is "5-2=3/2" at below codes(117 line).当以下代码(117 行)的“this.state.totalVal”为“5-2=3/2”时,会发生错误。

let result = Math.round(1000000000000 * eval(this.state.totalVal)) / 1000000000000;

And you can check values of this variable by setting a breakpoint at 117 line in the source tab of your browser devtools.您可以通过在浏览器开发工具的源选项卡中的 117 行设置断点来检查此变量的值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 停留在测试套件上的最后两个 FreeCodeCamp Javascript 计算器测试上? - Stuck on last two FreeCodeCamp Javascript Calculator tests on the test suite? qunit 与 jsmock 测试失败,我不知道为什么:) - qunit with jsmock test failing and I don't know why :) 我正在尝试将 Javascript 计算器作为 freecodecamp 任务的一部分,但无法使其与 ID 和数据运算符一起使用 - I'm trying to make a Javascript calculator as part of a freecodecamp task but can't get it to work with IDs and data-operators 单元测试失败不知道为什么 - Unit test failed don't know why 我无法在JavaScript中使用“ \\ n”,我不知道为什么它不起作用 - I can't use '\n" in javascript, I don't know why it isn't work 为什么我不能在JSFiddle中执行这个简单的JavaScript测试程序? - Why I can't perform this simple JavaScript test program into a JSFiddle? HTML和JavaScript,JavaScript没有输出,我不知道为什么? - HTML and JavaScript, no output from javascript, i don't know why? 我不知道如何解决成绩计算器功能 - I don't know how to fix my grade calculator function 我的地理位置(javascript)无法正常运行,我也不知道为什么 - my geolocation(javascript) does not work and I don't know why EventListener 不工作 JavaScript 我不知道为什么 - EventListener not working JavaScript and I don't know why
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM