Codecademy Forums

Mistake in setting values for Functions as Parameters

Hello everyone,

I thought the below made sense until I pasted val = 2 into the consistency function and I hit run a few times and it returns numbers like 3, 4, 5… basically for each run a different result. I would expect the val=2 essentially takes the place in addTwo(val=2) and therefore, it should always return 4!

Can anyone help explain please where might be mistake?

// Consistency function evaluates first and second runs are the same
const checkConsistentOutput = (func, val) => {
let firstTry = func(val);
let secondTry = func(val);
if (firstTry === secondTry) {
return firstTry;
} else {
return ‘This function returned inconsistent results’;
}
};

// addTwo function
const addTwo = num => num + 2;

// func = addTwo, val = 2
checkConsistentOutput(addTwo, 2); // returns different values for each run

If we set val = 2 outside of the function, and write val as a formal parameter it will shadow the variable and take the value given in the argument. They are not the same variable.