Advertisement
Guest User

Untitled

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