-1

I'm new to coding so apologies for the really basic question.

I have made a javascript function called ErrorType that checks whether my variables 'correct' and 'response' equal 70 and 68 respectively. If they do, then I want to set 'correct_rule_wrong_resp' to 1.

My problem is that when I call the function, it has no effect on the value of 'correct_rule_wrong_resp'. Where am I going wrong?

    correct = 70;
    response = 68;
    correct_rule_wrong_resp = null;

    function ErrorType (a, b, c){
        if (correct == a && response == b){
            c = 1
        }
    }

    ErrorType (70, 68, correct_rule_wrong_resp)
  • https://stackoverflow.com/a/6605700/1871033 – CherryDT Mar 19 '21 at 10:19
  • 1
    Changing the value of a scalar parameter you passed into your function, will not change any variables outside of the function. You need to _return_ the value from your function, and then do `correct_rule_wrong_resp = ErrorType(…);` – CBroe Mar 19 '21 at 10:19

2 Answers2

0

// Constants
const correct = 70;
const response = 68;
// Variable
let correct_rule_wrong_resp = null;

// Function
function ErrorType(a, b){
  if(correct == a && response == b){
      // Return result to function caller 
      return 1
  }
}

// Call function and assign returned value to your variable
correct_rule_wrong_resp = ErrorType(70, 68, correct_rule_wrong_resp);

// Test
console.log(correct_rule_wrong_resp);
tarkh
  • 2,424
  • 1
  • 9
  • 12
0

Here you are not assing Any value to correct_rule_wrong_resp, You are just calling function by passing variable