GithubHelp home page GithubHelp logo

Comments (4)

lidasong avatar lidasong commented on September 15, 2024

我是没有考虑先输入%或者=的情况,这一点我再改进!
infinity后可以继续加减乘除得到的都还是infinity这个不行吗?
代码命名有问题,我会再进一步改进。代码重构会跟进!
谢谢寿寿学长指导

from fe-study.

raytaylorlin avatar raytaylorlin commented on September 15, 2024

infinity是一个类似error的情况,现实的计算器的处理方式是必须按AC来清空才可以继续输入,你参照windows自带计算器的处理方式

from fe-study.

lidasong avatar lidasong commented on September 15, 2024

好的!

from fe-study.

raytaylorlin avatar raytaylorlin commented on September 15, 2024

#20 JS代码重构得不错,懂得抽象出numberClicksymbolClick函数就算达到了了解重构的目的。请以后在写代码的时候随时随地保持重构代码的思考。当然,press函数还可以再精简,设想我现在要你再添加“sin”“log”等等10几个按钮来做一个科学计算器,那你岂不是每添加一个按钮,就得在HTML中添加onclick="press('sin')",例如下面这样:

function press() {
    buttonId = document.getElementById("result");

    //定义符号表
    var SYMBOL_LIST = ['+', '-', 'x', '/', '%'];

    //用内置的event.target来获取点击的按钮内容
    var value = event.target.innerHTML;
    console.log(value);

    //处理数字
    if(value >= '0' && value <= '9') {
        numberClick(value);
    } else if (SYMBOL_LIST.indexOf(value) > -1) {
        //处理符号
        symbolClick(value);
    } else {
        //处理其他
        switch(value) {
            case 'AC':
            //......
        }
    }
    //......

当然还有更高级代码健壮性更强的重构方法,用到Javascript的function对象来处理不同的功能,这里就不再展开了。

任务3完成

from fe-study.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.