html计算器的基本功能实现,javascript实现计算器功能

本文实例为大家分享了javascript实现计算器功能的具体代码,供大家参考,具体内容如下

67515bc21fdfd30e2e4ff3843778642b.png

问题描述:

1、除法操作时,如果被除数为0,则结果为0

2、结果如果为小数,最多保留小数点后两位,如2 / 3 =0.67(显示0.67),1 / 2 = 0.5(显示0.5)

百度笔试0329

body, ul, li,select {

margin: 0;

padding: 0;

box-sizing: border-box;

}

ul,li {list-style: none;}

.calculator {

max-width: 300px;

margin: 20px auto;

border: 1px solid #eee;

border-radius: 3px;

}

.cal-header {

font-size: 16px;

color: #333;

font-weight: bold;

height: 48px;

line-height: 48px;

border-bottom: 1px solid #eee;

text-align: center;

}

.cal-main {

font-size: 14px;

}

.cal-main .origin-value {

padding: 15px;

height: 40px;

line-height: 40px;

font-size: 20px;

text-align: right;

overflow: hidden;

text-overflow:ellipsis;

white-space: nowrap;

}

.cal-main .origin-type,

.cal-main .target-type {

padding-left: 5px;

width: 70px;

font-size: 14px;

height: 30px;

border: 1px solid #eee;

background-color: #fff;

vertical-align: middle;

margin-right: 10px;

border-radius: 3px;

}

.cal-keyboard {

overflow: hidden;

}

.cal-items {

overflow: hidden;

}

.cal-items li {

user-select: none;

float: left;

display: inline-block;

width: 75px;

height: 75px;

text-align: center;

line-height: 75px;

border-top: 1px solid #eee;

border-left: 1px solid #eee;

box-sizing: border-box;

}

li:nth-of-type(4n+1) {

border-left: none;

}

li[data-action=operator] {

background: #f5923e;

color: #fff;

}

.buttons {

float: left;

width: 75px;

}

.buttons .btn {

width: 75px;

background-color: #fff;

border-top: 1px solid #eee;

border-left: 1px solid #eee;

height: 150px;

line-height: 150px;

text-align: center;

}

.btn-esc {

color: #ff5a34;

}

.btn-backspace {

position: relative;

}

简易计算器

0
  • 7
  • 8
  • 9
  • ÷
  • 4
  • 5
  • 6
  • x
  • 1
  • 2
  • 3
  • -
  • 0
  • 清空
  • =
  • +

var Calculator = {

init: function () {

var that = this;

if (!that.isInited) {

that.isInited = true;

// 保存操作信息

// total: Number, 总的结果

// next: String, 下一个和 total 进行运算的数据

// action: String, 操作符号

that.data = {total: 0, next: '', action: ''};

that.bindEvent();

}

},

bindEvent: function () {

var that = this;

// 请补充代码:获取 .cal-keyboard 元素

var keyboardEl = document.getElementsByClassName('cal-keyboard')[0]

keyboardEl && keyboardEl.addEventListener('click', function (event) {

// 请补充代码:获取当前点击的dom元素

var target = event.target;

// 请补充代码:获取target的 data-action 值

var action = target.getAttribute('data-action');

// 请补充代码:获取target的内容

var value = target.innerHTML;

if (action === 'num' || action === 'operator') {

that.result(value, action === 'num');

}

});

},

result: function (action, isNum) {

var that = this;

var data = that.data;

if (isNum) {

data.next = data.next === '0' ? action : (data.next + action);

!data.action && (data.total = 0);

} else if (action === '清空') {

// 请补充代码:设置清空时的对应状态

data.total = 0;

data.next = '';

data.action = '';

} else if (action === '=') {

if (data.next || data.action) {

data.total = that.calculate(data.total, data.next, data.action);

data.next = '';

data.action = '';

}

} else if (!data.next) {

data.action = action;

} else if (data.action) {

data.total = that.calculate(data.total, data.next, data.action);

data.next = '';

data.action = action;

} else {

data.total = +data.next || 0;

data.next = '';

data.action = action;

}

// ���补充代码:获取 .origin-value 元素

var valEl = document.getElementsByClassName('origin-value')[0];

valEl && (valEl.innerHTML = data.next || data.total || '0');

},

calculate: function (n1, n2, operator) {

n1 = +n1 || 0;

n2 = +n2 || 0;

if (operator === '÷') {

// 请补充代码:获取除法的结果

if(n2 == 0 || n1 == 0) return 0

return Math.round((n1/n2)*100)/100;

} else if (operator === 'x') {

// 请补充代码:获取乘法的结果

return n1 * n2;

} else if (operator === '+') {

// 请补充代码:获取加法的结果

return n1 + n2;

} else if (operator === '-') {

// 请补充代码:获取减法的结果

return n1 - n2;

}

}

};

Calculator.init();

更多计算器功能实现,请点击专题: 计算器功能汇总 进行学习

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值