Home > Blockchain >  Javascript Basic calculator with parameters--not getting output
Javascript Basic calculator with parameters--not getting output

Time:01-03

below is my problem solving question.

Basic calculator with parameters In this lab, we will practice for loop with an array and functions and conditionals. Your task for this lab is to:

Create a function named performMath that accepts three parameters - number1, number2 and operation If operation is ' ', add the numbers and return the result If operation is '*', multiply the numbers and return the result

My code for aforementioned question,but am not getting correct result, please check and confirm do i code correctly?

let number1 =20
let number2 =10
function performMath(number1,number2){
return number1   number2
}
console.log(performMath(number1   number2));

CodePudding user response:

You must use three arguments as your assignment says:

let number1 =20
let number2 =10
let operation = ' '

function performMath(num1,num2,oper){
  if(oper === '-')
    return num1 - num2
  else if(oper === ' ')
    return num1   num2
  else if(oper === '*')
    return num1 * num2
}

console.log(performMath(number1, number2, '-'));
console.log(performMath(number1, number2, ' '));
console.log(performMath(number1, number2, '*'));

CodePudding user response:

function performMath(num1, num2, calc){
    if (calc === ' ') return num1   num2;
    if (calc === '-') return num1 - num2;
    if (calc === '*') return num1 * num2;
    if (calc === '/') return num1 / num2;
    return "something went wrong";
}
console.log(performMath(12, 10, " "));
console.log(performMath(4, 3, "*"));
  • Related