如何解决如何做一个简单的计算器
任务是实现一个功能,一个简单的加减法计算器,该计算器:
- 接受三个参数,两个整数,firstArg,SecondArg和一个字符串操作,该字符串可以是“ +”或“-”
- 根据操作返回值: 如果运算为+,则返回firstArg与secondArg的和。 如果运算为-,则ir返回firstArg与secondArg的减法。 如果操作不无效,则返回“无效操作”
有人可以帮忙吗?
解决方法
function name(a0,a1,a2) {
return a2 === "+"? a0 + a1: a0 - a1;
}
,
这是您所要求的功能:
const calculator = function(firstArg,secondArg,operationType) {
if (operationType === '+') {
return firstArg + secondArg;
} else if (operationType === '-') {
return firstArg - secondArg;
}
}
,
下面是您所要求的功能。可以通过JavaScript了解关于here的switch语句。如果您想使用某些方法,可能会更简单一些,请查看其他答案。
<template>
<ul class="tag-list">
<li
class="tag-default tag-pill tag-outline"
v-for="(tag,index) of tags"
:key="index"
>
<span v-text="tag" />
</li>
</ul>
</template>
<script>
export default {
name: "TagList",props: {
tags: Array
}
};
</script>
,
function process (operation,a,b) {
if(operation === '+'){
return a + b;
}else if(operation === '-'){
return a - b;
}else{
return "invalid operation";
}
}
console.log( process('+',3,5) );
,
您需要使用js eval 函数
function simpleAdditionAndSubtractionCalculator(num1,num2,operatorType) {
return eval(num1 + operatorType + num2)
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。