Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

Arithmetic

/*
Given two numbers and an arithmetic operator (the name of it, as a string), 
return the result of the two numbers having that operator used on them.

a and b will both be positive integers, and a will always be the first number 
in the operation, and b always the second.

The four operators are "add", "subtract", "divide", "multiply".

A few examples:(Input1, Input2, Input3 --> Output)
  5, 2, "add"      --> 7
  5, 2, "subtract" --> 3
  5, 2, "multiply" --> 10
  5, 2, "divide"   --> 2.5
*/

const arithmetic = (a, b, operator) => {
  let resultNum = 0
    switch (operator) {
      case "add":
        resultNum = a + b;
        break;
      case "subtract":
        resultNum = a - b;
        break;
      case "multiply":
        resultNum = a * b;
        break;
      case "divide":
        resultNum = a / b;
        break;
      default:
        resultNum = 0;
    }
  return resultNum
}

// With love @kouqhar
Source by www.codewars.com #
 
PREVIOUS NEXT
Tagged: #Arithmetic
ADD COMMENT
Topic
Name
8+5 =