如何做一个简单的计算器

问题描述

任务是实现一个功能一个简单的加减法计算器,该计算器:

  • 接受三个参数,两个整数,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)
}