Comments (4)
我是没有考虑先输入%或者=的情况,这一点我再改进!
infinity后可以继续加减乘除得到的都还是infinity这个不行吗?
代码命名有问题,我会再进一步改进。代码重构会跟进!
谢谢寿寿学长指导
from fe-study.
infinity是一个类似error的情况,现实的计算器的处理方式是必须按AC来清空才可以继续输入,你参照windows自带计算器的处理方式
from fe-study.
好的!
from fe-study.
#20 JS代码重构得不错,懂得抽象出numberClick
和symbolClick
函数就算达到了了解重构的目的。请以后在写代码的时候随时随地保持重构代码的思考。当然,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)
- 王远涵Calculator任务反馈 HOT 1
- 蔡孝直Blog任务反馈 HOT 1
- 王睿Calculator任务反馈 HOT 1
- 杨楠Calculator任务反馈 HOT 2
- 曾祥意Blog(PHP)任务反馈
- 蔡孝直Calculator任务反馈 HOT 1
- 张言BLOG(PHP)任务反馈
- 孔思聪BLOG(PHP)任务反馈 HOT 2
- 杨楠BLOG(PHP)任务反馈 HOT 1
- 孔思聪Puzzle8任务反馈 HOT 1
- 王睿Puzzle8任务反馈 HOT 2
- 蔡孝直BLOG(PHP)任务反馈 HOT 1
- 杨楠Puzzle8任务反馈 HOT 1
- 王远涵BLOG(PHP)任务反馈
- 曾祥意Puzzle8任务反馈 HOT 1
- 王远涵Puzzle8任务反馈 HOT 1
- 王佳静任务反馈 HOT 8
- 袁晓梅任务反馈 HOT 5
- 一个外来人来点star
- Potential Security Vulnerability Found - Requesting Secure Contact Method HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from fe-study.