Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // EXERCISE 2
- // // extend our awesome calc function by adding some conditions...
- // // in case the third arguments is / or * and the second argument is not provided, the second argument should default to one.
- // // in case the third arguments is + or - and the second argument is not provided, the second argument should default to zero.
- // //Example:
- // calc(10,"/") //10
- // calc (30,"*") //30
- // calc (2,"+") //2
- // calc (3,"-") //3
- function calc (num1, num2, operator) {
- if(!num2 && operator === '/' || operator === '*'){
- num2 = 1;
- }
- if(!num2 && operator === '+'|| operator === '-'){
- num2 = 0;
- }
- if (operator === '+') {
- return num1 + num2;
- } else if (operator === '-') {
- return num1 - num2;
- } else if (operator === '*') {
- return num1 * num2;
- } else if (operator === '/') {
- return num1 / num2;
- } else {
- return 'wrong data provided';
- }
- };
- module.exports = {
- calc
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement