Tim Rooke
Tim Rooke

Reputation: 376

Function being called twice - once with correct value, once with wrong value

This is a first post and also I am new to coding, so hopefully the question is clear enough.

I am doing a basic JavaScript exercise. The program is working largely as I need it to except one problem.

The program populates a series of alerts. For example, if you select maths and then enter: 1, +, 1 in the next three alerts it returns "Your answer is 2" (which is correct). However, then, it subsequently show the 'Uh oh! Problem!!', which isn't right.

I can't work out why this is happening?

I have pasted the relevant code below. Also, a https://jsfiddle.net/poc4kzm9/ with all the code is here.

//////////////// MATHS CALCULATOR //////////////////////////

// logic for maths calculation
function mathCalculate(operator, firstNumber, secondNumber) {
  console.log(operator);
  if (operator === '+') {
    add(firstNumber, secondNumber);
  } else if (operator === '-') {
    subtract(firstNumber, secondNumber);
  } else if (operator === '/') {
    divide(firstNumber, secondNumber);
  } else if (operator === '*') {
    multiply(firstNumber, secondNumber);
  } else if (operator === 'sq') {
    power(firstNumber, secondNumber);
  } else if (operator ==='sqrt') {
    squareRoot(firstNumber);
  } else {
    alert('Uh oh! Problem!!');
  }
}

// collect required values for math
function mathSetup() {
  // Set intial values for math calculator
  const firstNumber = prompt('What is your first number?');
  const operator = prompt('Which operator would you like to use? (+, -, /, *, sq, sqrt)');
  let secondNumber;

  // No need for a second number if sqrt-ing the first value
  if (operator !== 'sqrt') {
    secondNumber = prompt('What is your second number?');
  }
  mathCalculate(operator, firstNumber, secondNumber);
}

// The various functions by operator, one for each operator
function add(a,b) {
  alert('Answer is ' + (parseFloat(a) + parseFloat(b)));
}

Any help would be appreciated.

Upvotes: 0

Views: 214

Answers (2)

Sharikov Vladislav
Sharikov Vladislav

Reputation: 7269

You call mathCalculate method twice.

First time is in mathSetup and another one is your main loop in switch. Second time you call it without parameters. Thats why you get alert about problem.

To fix this you have to remove matchCalculate from switch. Another way is to return data from mathSetup and use it in mathCalculate

I also made fiddle: https://jsfiddle.net/5bh9ndhc/

I returned data from mathSetup and I use it in mathCalculate:

const [operator, firstNumber, secondNumber] = mathSetup();
mathCalculate(operator, firstNumber, secondNumber);

Note that I used destructuring assignmen. This syntax is part of ECMAScript 2015 so it will work only in a few modern browsers.

UPD: I highly recommend use debugger keyword in such cases. Your code has many lines and I see it first time in my life, but because of debugger keyword and Chrome Devtools I was able to find the mistake in 30 seconds.

Upvotes: 2

ayusha
ayusha

Reputation: 484

You are calling calculate function twice

One inside the setup function

function mathSetup() {
  // Set intial values for math calculator
  const firstNumber = prompt('What is your first number?');
  const operator = prompt('Which operator would you like to use? (+, -, /, *, sq, sqrt)');
  let secondNumber;

  // No need for a second number if sqrt-ing the first value
  if (operator !== 'sqrt') {
    secondNumber = prompt('What is your second number?');
  }
  mathCalculate(operator, firstNumber, secondNumber);
}

and after setup

  mathSetup();
  mathCalculate();

Remove calculate function from switch case at that point you don't have information about calculation. Fiddle link - https://jsfiddle.net/ezmhc0ff/

Upvotes: 0

Related Questions