Home > Blockchain >  How to pass a false conditional statement in a function parameter to turn it into true
How to pass a false conditional statement in a function parameter to turn it into true

Time:11-08

I am making a function which takes a conditional statement.

function validate(condition){
  .../
}

I want to make this function such that if i input a false statement, it will turn it into true.For Example:

var a = 2;
validate(a == 3); //It turns "a" into 3;

Is it even possible to do that!

I would be glab if someone could help me.

CodePudding user response:

You should pass the new value instead of the result of the comparison so that you'll be able to change the value of a to the new value.

const validate = (newVal) =>
  a === newVal ? a : a = newVal

var a = 2;
validate(3); //It turns "a" into 3;
console.log(a)

CodePudding user response:

No, it isn't.

a == 3 is evaluated in place and the result is passed to the function (so the function knowns nothing about the expression).

Even if that wasn't the case, variables are passed by value, so it wouldn't know anything about a either.

CodePudding user response:

In this case you should pass the variable and the comparison value.

var a = 2;
function validate(variable, conditional) {
  if (variable != conditional) return conditional
}; 

a = validate(a, 3)
console.log(a)

  • Related