Advertisement
Guest User

Untitled

a guest
Aug 22nd, 2019
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.98 KB | None | 0 0
  1. // EXERCISE 2
  2.  
  3. // // extend our awesome calc function by adding some conditions...
  4. // // in case the third arguments is / or * and the second argument is not provided, the second argument should default to one.
  5. // // in case the third arguments is + or - and the second argument is not provided, the second argument should default to zero.
  6.  
  7. // //Example:
  8.  
  9. // calc(10,"/") //10
  10. // calc (30,"*") //30
  11. // calc (2,"+") //2
  12. // calc (3,"-") //3
  13.  
  14.  
  15. function calc (num1, num2, operator) {
  16.  
  17. if(!num2 && operator === '/' || operator === '*'){
  18.  
  19. num2 = 1;
  20.  
  21. }
  22.  
  23. if(!num2 && operator === '+'|| operator === '-'){
  24.  
  25. num2 = 0;
  26.  
  27. }
  28.  
  29. if (operator === '+') {
  30.  
  31. return num1 + num2;
  32.  
  33. } else if (operator === '-') {
  34.  
  35. return num1 - num2;
  36.  
  37. } else if (operator === '*') {
  38.  
  39. return num1 * num2;
  40.  
  41. } else if (operator === '/') {
  42.  
  43. return num1 / num2;
  44.  
  45. } else {
  46.  
  47. return 'wrong data provided';
  48. }
  49. };
  50.  
  51. module.exports = {
  52. calc
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement