题目:

vue代码

 <template>
<div class="colculate">
<div>
<select v-model="num1" placeholder="数字1">
<option
v-for="item in numLimitList"
:key="item"
:label="item"
:value="item">
</option>
</select>
<select v-model="checkOperate" placeholder="处理符">
<option
v-for="item in operateFlag"
:key="item"
:label="item"
:value="item">
</option>
</select>
<select v-model="checkNum2" placeholder="数字2">
<option
v-for="item in numLimitList"
:key="item"
:label="item"
:value="item">
</option>
</select>
<button class="colculate-btn" @click="colculateNum">Colculate</button>
</div>
<div>Answer: <span class="show-result"> {{value}} </span></div>
</div>
</template> <script lang="ts">
import { Component, Prop, Vue } from "vue-property-decorator"; const operateFlag = ['+', '-', '*', '/']
const numLimitList = Array(100).fill('').map((_, v) => v) @Component
export default class Colculate extends Vue {
@Prop() private msg!: string;
message: string = 'Hello!'
operateFlag: string[] = operateFlag
numLimitList: number[] = numLimitList
num1: number = 0
num2: number = 0
operate: string = '+'
value: any = ''
set checkOperate(nVal: string) {
if (this.num2 === 0 && nVal === '/') {
this.value = '除数不能为0'
} else {
this.operate = nVal
}
}
get checkOperate() {
return this.operate
}
set checkNum2(nVal: number) {
if (this.operate === '/' && nVal === 0) {
this.value = '除数不能为0'
} else {
this.num2 = nVal
}
}
get checkNum2() {
return this.num2
}
colculateNum (): void {
switch (this.operate) {
case '+': this.value = this.num1 + this.num2; break
case '-': this.value = this.num1 - this.num2; break
case '*': this.value = this.num1 * this.num2; break
case '/':
if (this.num2 === 0) {
this.value = '除数不能为0'
} else {
this.value = this.num1 / this.num2;
}
break
default: this.value = '错误的操作符'
}
}
}
</script> <style lang="scss" scoped>
.colculate {}
</style>

详解:

功能-->计算num1和num2不同操作[+-*/]的计算式的结果,

num1-->计算式的第一个值,不需要做任何处理

num2-->计算式的第二个值,注意点:当是除法时,num2不能为0,如果用户操作改为0,则会提示用户‘除数不能为0’

operate-->计算式的操作符,注意点:当num2为0时,如果用户改操作符为'/'时,则会提示用户‘除数不能为0’

点击计算Colculate按钮时计算组成式子的结果,并显示出来

代码:

1、准备操作符的改变时的检查

 set checkOperate(nVal: string) {
if (this.num2 === 0 && nVal === '/') {
this.value = '除数不能为0'
} else {
this.operate = nVal
}
}

2、准备num2值改变时的检查

 set checkNum2(nVal: number) {
if (this.operate === '/' && nVal === 0) {
this.value = '除数不能为0'
} else {
this.num2 = nVal
}
}

3、计算值的方法

 colculateNum (): void {
switch (this.operate) {
case '+': this.value = this.num1 + this.num2 + ''; break
case '-': this.value = this.num1 - this.num2 + ''; break
case '*': this.value = this.num1 * this.num2 + ''; break
case '/':
if (this.num2 === 0) {
this.value = '除数不能为0'
} else {
this.value = this.num1 / this.num2 + '';
}
break
default: this.value = '错误的操作符'
}
}

实际操作:

计算实时处理验证为0的情况

单元测试:

 import { shallowMount } from "@vue/test-utils";
import Colculate from "@/components/Colculate.vue"; describe("Colculate.vue", () => {
it("计算属性是否正确", () => {
const wrapper = shallowMount(Colculate);
wrapper.setData({ num1: 10 });
wrapper.setData({ operate: "*" });
wrapper.setData({ num2: 12 });
const button = wrapper.find(".colculate-btn");
button.trigger("click");
expect(wrapper.vm.$data.value).toEqual("120");
});
// 一般情况下不会出现这种情况
it("当除数为0时,返回结果是除数不能为0", () => {
const wrapper = shallowMount(Colculate);
wrapper.setData({ num1: 10 });
wrapper.setData({ operate: "/" });
wrapper.setData({ num2: 0 });
const button = wrapper.find(".colculate-btn");
button.trigger("click");
expect(wrapper.vm.$data.value).toEqual("除数不能为0");
});
it("[改除数为0时]当除数为0时,返回结果是除数不能为0", () => {
const wrapper = shallowMount(Colculate);
wrapper.setData({ num1: 10 });
wrapper.setData({ operate: "/" });
wrapper.setData({ num2: 5 });
const button = wrapper.find(".colculate-btn");
button.trigger("click");
expect(wrapper.vm.$data.value).toEqual("2");
wrapper.setData({ num2: 0 });
button.trigger("click");
expect(wrapper.vm.$data.value).toEqual("除数不能为0");
});
it("[改操作符为除法时]当除数为0时,返回结果是除数不能为0", () => {
const wrapper = shallowMount(Colculate);
wrapper.setData({ num1: 10 });
wrapper.setData({ operate: "*" });
wrapper.setData({ num2: 0 });
const button = wrapper.find(".colculate-btn");
button.trigger("click");
expect(wrapper.vm.$data.value).toEqual("0");
wrapper.setData({ operate: "/" });
button.trigger("click");
expect(wrapper.vm.$data.value).toEqual("除数不能为0");
});
});

结果

react

import React, { Component } from 'react'

class App extends Component {
constructor() {
super()
this.state = {
showValue: '',
operate: '+',
num1: 0,
num2: 0
}
}
/**
* 修改数字1
*/
handleChangeNum1 = e => {
this.setState({
num1: Number(e.target.value)
})
}
/**
* 修改操作符
*/
handleChangeOperate = e => {
this.setState({
operate: e.target.value
})
}
/**
* 修改数字2
*/
handleChangeNum2 = e => {
this.setState({
num2: Number(e.target.value)
})
}
/**
* 计算计算式
*/
colculateNum = () => {
let showValue = ''
let { num1, num2, operate } = this.state
switch (operate) {
case '+':
showValue = num1 + num2
break
case '-':
showValue = num1 - num2
break
case '*':
showValue = num1 * num2
break
case '/':
if (num2 === 0) {
showValue = '除数不能为0'
} else {
showValue = num1 / num2
}
break
default:
showValue = '错误的操作符'
}
this.setState({
showValue
})
}
render() {
const operateFlag = ['+', '-', '*', '/']
const numLimitList = Array(100)
.fill('')
.map((_, v) => v)
return (
<div className="colculate">
<div>
<select title="数字1" value={this.state.num1} onChange={this.handleChangeNum1}>
{numLimitList.map(function(item) {
return <option value={item} label={item} key={item} />
})}
</select>
<select title="操作符" value={this.state.operate} onChange={this.handleChangeOperate}>
{operateFlag.map(function(item) {
return <option value={item} label={item} key={item} />
})}
</select>
<select title="数字2" value={this.state.num2} onChange={this.handleChangeNum2}>
{numLimitList.map(function(item) {
return <option value={item} label={item} key={item} />
})}
</select>
<button style={{ marginLeft: '10px' }} onClick={this.colculateNum}>
Colculate
</button>
</div>
<div>
Answer: <span className="show-result">{this.state.showValue}</span>
</div>
</div>
)
}
} export default App

一道笔试题(vue,react)的更多相关文章

  1. Java中有关构造函数的一道笔试题解析

    Java中有关构造函数的一道笔试题解析 1.详细题目例如以下 下列说法正确的有() A. class中的constructor不可省略 B. constructor必须与class同名,但方法不能与c ...

  2. 一道笔试题来理顺Java中的值传递和引用传递

      题目如下: private static void change(StringBuffer str11, StringBuffer str12) { str12 = str11; str11 = ...

  3. 一道笔试题和UML思想 ~

    一句软件工程界的名言,让我想起了一个和一道笔试题有关的故事.希望更多的人了解 UML 背后的思想比他的语法更重要,是笔者写作本文的一点小愿望. 一.从一句软件工程名言说起 对很多事情的处理上,东西方都 ...

  4. 由阿里巴巴一道笔试题看Java静态代码块、静态函数、动态代码块、构造函数等的执行顺序

    一.阿里巴巴笔试题: public class Test { public static int k = 0; public static Test t1 = new Test("t1&qu ...

  5. 转:一道笔试题-将int型数组强制转换为char*,再求strlen,涉及大小端

    写出如下程序运行结果: #include<stdio.h> #include<string.h> int main() { int a[2000]; char *p = (ch ...

  6. golang 中 string 转换 []byte 的一道笔试题

    背景 去面试的时候遇到一道和 string 相关的题目,记录一下用到的知识点.题目如下: s:="123" ps:=&s b:=[]byte(s) pb:=&b s ...

  7. 通过一道笔试题浅谈javascript中的promise对象

    因为前几天做了一个promise对象捕获错误的面试题目,所以这几天又重温了一下promise对象.现在借这道题来分享下一些很基础的知识点. 下面是一个面试题目,三个promise对象捕获错误的例子,返 ...

  8. IGT一道笔试题

    1到n连续的n个数 输入m 得出m个有序序列 比如 输入为n=5 ,m=3 则输出 543 542 541 532 531 521  432 431 421 321 当前长度为i,每个位上的取之范围为 ...

  9. 一道笔试题:给定编码规则,实现decode()方法

    public class CodeDecode {     /*变换函数encode()顺序考察已知字符串的字符,按以下规则逐组生成新字符串:       (1)若已知字符串的当前字符不是大于0的数字 ...

随机推荐

  1. cogs2550. 冰桥,升起来了!

    [问题背景] 11月16日: 今天要来到南极洲的一角来考察啦!南极的空气真的很好呢,只不过有点冷,雪什么的真是太可爱了!这次我要在一个冰谷(应该说是山谷的地方)考察,考察点在这山谷的两边(希望不要掉下 ...

  2. JNDI-Injection-Exploit

    介绍 最近把自己之前写的JNDI注入利用工具改了一下push到了github,地址:https://github.com/welk1n/JNDI-Injection-Exploit,启动后这个工具开启 ...

  3. C语言--最大公约数

    //辗转相除法 int main() { int a,b; int t; scanf("%d %d", &a,&b); ) { t = a%b; a = b; b ...

  4. Python制作有道翻译小工具

    该工具主要是利用了爬虫,爬取web有道翻译的内容. 然后利用简易GUI来可视化结果. 首先我们进入有道词典的首页,并点击翻译结果的审查元素 之后request响应网页,并分析网页,定位到翻译结果. 使 ...

  5. 记录JS如何使用广度遍历找到节点的所有父节点

    我们在实际的工作业务场景中经常遇到这样的场景,求取树数据中某个节点的父亲节点以及所有的父亲节点,这样的场景下不建议使用深度遍历,使用广度遍历可以更快找到. 1.案例解说 比如树的长相是这样的: 树的数 ...

  6. Web渗透之mssql差异备份getshell

    简介 差异备份数据库得到webshell.在sql server 里dbo和sa权限都有备份数据库权限,我们可以把数据库备份称asp文件,这样我们就可以通过mssqlserver的备份数据库功能生成一 ...

  7. CDN实现原理

    避让:尽可能避开互联网上有可能影响数据传输速度和稳定性的瓶颈和环节,使内容传输的更快.更稳定. 检测:通过在网络各处放置节点服务器所构成的在现有的互联网基础之上的一层智能虚拟网络,CDN系统能够实时监 ...

  8. vue——前端跨域

    ***针对的是不同域名.不同协议的跨域: 1.找到config文件中开发环境的配置文件——dev.env.js,在里面将要跨域的域名配置进去 2.找到config文件中线上环境的配置文件——prod. ...

  9. pytest中unicode编码问题(如test_fix.py::Test1::test_s1[\u6d4b\u8bd5-\u6d4b\u8bd5])

    现象: 采用如下方式可将其正确显示为中文 ss = r"test_fix.py::Test1::test_s1[\u6d4b\u8bd5-\u6d4b\u8bd5]" print( ...

  10. Spring Boot项目中如何定制PropertyEditors

    本文首发于个人网站:Spring Boot项目中如何定制PropertyEditors 在Spring Boot: 定制HTTP消息转换器一文中我们学习了如何配置消息转换器用于HTTP请求和响应数据, ...