# Mad Mathematician's Calculator (Basic Version)

You have to write a `calc(a,b,operator)` function to receive 2 numbers and 1 operator respectively, assuming `a` , `b` , and `operator` . `calc` function will calculate a result from `a` and `b` as indicated by `operator` and return as a number.

### Coding Limitation:

The data listed below are not allowed in your code:

• Operators
• Mathematical operators: `+` , `-` , `*` , `/` , `%`
• Bitwise operators: `<<` , `>>` , `^` , `~` , `!` , `|` , `&`
• Other operators: `<` , `>` , `.`
###### Note: arrow function is allowed

The modules/functions below are also disabled:

• Bypassing functions: `require()` , `eval()` , function constructor
• Time functions: `setTimeout()` , `setInterval()`
• Modules: `fs` , `vm`

### Input:

• `a` , `b` as integer from 0 to 5000
• `operator` as string of basic mathematical operator as follow:
• `"+"` addition
• `"-"` substraction
• `"*"` multiplication
• `"/"` division
• `"%"` modulus

### Output:

• return a number with maximum 2 decimal places (rounded down), or `NaN` if `a` /0 or `a` %0

Is there any specific place where you need help? What code have you written so far?

i don’t have any code, i asked help, because i don’t know how to solve this task

We’re happy to help, but you have to give us something to help you with. We won’t do your homework for you.

maybe you can give me some advises? I already tried to solve this task. I wanted to make a string like: ‘5 + 5’ , and when convert it to mathematical expression. But i don’t know how to do this without eval(). How do you think ,
can i solve this problem like this?

All you really need to do is write a `switch` statement with one line per operator. I’ll give you one to start:

`case '+': return a + b`

The rest should be along the same lines.

I suspect the next challenge is going to be writing an expression parser, in which you’ll re-use that `calc` function. But let’s not get ahead of ourselves … Did you see a task? I can not use ‘+’ or other operators in my code. Please check all task

The data listed below are not allowed in your code:

• Operators
• Mathematical operators: `+` , `-` , `*` , `/` , `%`
• Bitwise operators: `<<` , `>>` , `^` , `~` , `!` , `|` , `&`
• Other operators: `<` , `>` , `.`
[/quote]

### Example:

``````calc(1, 2, "+") === 3 //should return true
calc(0, 0, "-") === 0 //should return true
calc(6, 7, "*") === 42 //should return true
calc(5, 4, "%") === 1 //should return true
isNaN(calc(9, 0, "/")) === true //should return true
``````

The assignment is patently unreasonable. I don’t see how you’re expected to implement this without using any operators at all. Forbidding every single numeric operator in the language in fact makes the assignment impossible under those restrictions.

I glossed over that part because my brain has a sanity filter that’s allowed me to survive 2020.

1 Like

this task from https://www.codewars.com/, it’s possible, i can give you a link on this kata. https://www.codewars.com/kata/5a60b017fd56cb469e000279/javascript

I’m sure there’s some weird clever lateral-thinking trick that could solve this challenge. I prefer reality. Codewars is the biggest waste of time since broadcast television.

1 Like

You need to convert the number into another more complex object, one which has methods for adding together two of those objects and for subtracting things from one of those objects.

The trick is use arrays. Addition is concatenation. Subtraction can be done with recursion and `shift` (the constraints you gave mean you can’t use loops). Multiply is just addition. Division is just subtraction. Once you have all four you can implement any other maths operation.

It’s pretty stupid, and I don’t really like things like this that are unfair tricks either (don’t use these language things, just use abstractions that are built on top of them!), but it’s just a daft puzzle so I guess no harm done. Codewars does have a lot of these kind of things though, things that are based on tricks. I don’t think it’s a good practice site tbh, definitely not for people learning (exercism etc are much much better for this).

1 Like

And here my mind was going to lambda calculus Church arithmetic, but still couldn’t figure how to convert them back to numbers without operators. Let alone figure out how to do bit-shifts (er well I guess that’s just multiplying/dividing by 2).

I was never good at brainteasers as a kid either.

Haha, I only knew because I remembered reading something years ago where someone did it for fun, I’m not so smart – I was baffled (I thought bit shift straightaway, and that’s not allowed) until that vague memory surfaced, and then it just sorta clicked as soon as I could see how add would work.