Search
 
SCRIPT & CODE EXAMPLE
 

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
Comment

PREVIOUS NEXT
Code Example
Javascript :: Assigning A Property The Return Value Of A Function In Class 
Javascript :: cubing timer 
Javascript :: blob to wav javascript 
Javascript :: prisma count non-null 
Javascript :: add array and sort 
Javascript :: Recursion In A Class Function 
Javascript :: javascript hide div 
Javascript :: var oddOrEven = function(num) {}; 
Javascript :: isPowerOfTow 
Javascript :: How to sum to small numbers 
Javascript :: NavBar with divs 
Javascript :: Watch an API for Updates 
Javascript :: Backbone View El 
Javascript :: Backbone Collection 
Javascript :: strictPopulate 
Javascript :: merge large arrays 
Javascript :: selling partner api node install 
Javascript :: javascript reducers 
Javascript :: insert element in array javascript 
Javascript :: javascript get object methods 
Javascript :: why null is object in javascript 
Javascript :: 2d arrays js 
Javascript :: subarray javascript 
Javascript :: react native get screen height and width 
Javascript :: suitescript dialog box 
Javascript :: The DOM Parent-Child Relationship 
Javascript :: JavaScript built-in methods 
Javascript :: javascript Nested Destructuring Assignment 
Javascript :: JavaScript HTML DOM Node Lists 
Javascript :: Knockout js custom bindings 
ADD CONTENT
Topic
Content
Source link
Name
9+6 =