-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharithmetic.js
More file actions
32 lines (25 loc) · 1.17 KB
/
arithmetic.js
File metadata and controls
32 lines (25 loc) · 1.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
//Make a function that does 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
// Try to do it without using if statements!
function arithmetic(a, b, operator){
if(operator === "add"){
return a+b
}else if (operator === "subtract"){
return a-b
}else if(operator === "multiply"){
return a*b
}else{
return a/b
}
}
console.log(arithmetic(1, 2, "add"))// 3, "'add' should return the two numbers added together!");
console.log(arithmetic(8, 2, "subtract"))// 6, "'subtract' should return a minus b!");
console.log(arithmetic(5, 2, "multiply"))// 10, "'multiply' should return a multiplied by b!");
console.log(arithmetic(8, 2, "divide"))// 4, "'divide' should return a divided by b!");