7 kyu

Basic Calculator

6,300 of 10,531TheDoctor

Description:

Write a function called calculate that takes 3 values. The first and third values are numbers. The second value is a character. If the character is "+" , "-", "*", or "/", the function will return the result of the corresponding mathematical function on the two numbers. If the string is not one of the specified characters, the function should return null (throw an ArgumentException in C#).

calculate(2,"+", 4); //Should return 6
calculate(6,"-", 1.5); //Should return 4.5
calculate(-4,"*", 8); //Should return -32
calculate(49,"/", -7); //Should return -7
calculate(8,"m", 2); //Should return null
calculate(4,"/",0) //should return null

Keep in mind, you cannot divide by zero. If an attempt to divide by zero is made, return null (throw an ArgumentException in C#)/(None in Python).

Fundamentals

More By Author:

Check out these other kata created by TheDoctor

Stats:

CreatedNov 27, 2013
PublishedNov 27, 2013
Warriors Trained14176
Total Skips654
Total Code Submissions29579
Total Times Completed10531
JavaScript Completions6300
Java Completions1331
C# Completions556
Python Completions2482
Total Stars115
% of votes with a positive feedback rating91% of 1475
Total "Very Satisfied" Votes1248
Total "Somewhat Satisfied" Votes196
Total "Not Satisfied" Votes31
Ad
Contributors
  • TheDoctor Avatar
  • jhoffner Avatar
  • Javatlacati Avatar
  • coder4fun Avatar
  • Voile Avatar
  • Souzooka Avatar
  • rowcased Avatar
  • hobovsky Avatar
  • albertogcmr Avatar
  • rge123 Avatar
Ad