Skip to content

Commit 7322583

Browse files
author
juanantonioledesma
committed
Add "Make a function that does arithmetic!" kata
1 parent 0ed5824 commit 7322583

File tree

2 files changed

+56
-0
lines changed

2 files changed

+56
-0
lines changed
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<h1>Make a function that does arithmetic! <sup><sup>7 Kyu</sup></sup></h1>
2+
3+
<sup>
4+
<a href="https://www.codewars.com/kata/583f158ea20cfcbeb400000a">
5+
<strong>LINK TO THE KATA</strong>
6+
</a> - <code>FUNDAMENTALS</code>
7+
</sup>
8+
9+
## Description
10+
11+
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.
12+
13+
`a` and `b` will both be positive integers, and `a` will always be the first number in the operation, and b always the second.
14+
15+
The four operators are "add", "subtract", "divide", "multiply".
16+
17+
A few **examples:(Input1, Input2, Input3 --> Output)**
18+
19+
```
20+
5, 2, "add" --> 7
21+
5, 2, "subtract" --> 3
22+
5, 2, "multiply" --> 10
23+
5, 2, "divide" --> 2.5
24+
```
25+
26+
Try to do it without using if statements!
27+
28+
## Solutions
29+
30+
```javascript
31+
const arithmetic = (a, b, operator) => {
32+
const operations = {
33+
add: a + b,
34+
subtract: a - b,
35+
multiply: a * b,
36+
divide: a / b,
37+
}
38+
39+
return operations[operator]
40+
}
41+
```
42+
43+
---
44+
45+
```javascript
46+
const arithmetic = (a, b, operator) => {
47+
return operator === 'add'
48+
? a + b
49+
: operator === 'subtract'
50+
? a - b
51+
: operator === 'multiply'
52+
? a * b
53+
: a / b
54+
}
55+
```

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ JavaScript katas on Codewars are programming challenges that help you improve yo
117117
- **[Isograms](./7-kyu/isograms.md)**<br><sup>`STRINGS` `FUNDAMENTALS`</sup>
118118
- **[L2: Triple X](./7-kyu/l2-triple-x.md)**<br><sup>`STRINGS` `REGULAR EXPRESSIONS` `FUNDAMENTALS`</sup>
119119
- **[Letterbox Paint-Squad](./7-kyu/letterbox-paint-squad.md)**<br><sup>`FUNDAMENTALS`</sup>
120+
- **[Make a function that does arithmetic!](./7-kyu/make-a-function-that-does-arithmetic.md)**<br><sup>`FUNDAMENTALS`</sup>
120121
- **[Merge two arrays](./7-kyu/merge-two-arrays.md)**<br><sup>`FUNDAMENTALS` `ARRAYS`</sup>
121122
- **[Return the closest number multiple of 10](./7-kyu/return-the-closest-number-multiple-of-10.md)**<br><sup>`MATHEMATICS` `FUNDAMENTALS`</sup>
122123
- **[Sort Numbers](./7-kyu/sort-numbers.md)**<br><sup>`FUNDAMENTALS`</sup>

0 commit comments

Comments
 (0)