Codecademy Forums

Do I need to use addOneToOne as the parameter?

I have a few questions. Why do we have to put addOneToOne as a parameter? What if remove it as a parameter and still call it? Does timeFuncRuntime count how long it takes to actually run the function or the time between when it is a parameter and it is called?

const timeFuncRuntime = addOneToOne => {
let t1 = Date.now();
addOneToOne();
let t2 = Date.now();
return t2 - t1;
}

const addOneToOne = () => 1 + 1;

you shouldn’t. The advantage of timeFuncRuntime is that we can calculate the runtime of any function by passing this function as argument when we call timeFuncRuntime:

console.log(timeFuncRuntime(addOneToOne));
console.log(timeFuncRuntime(checkThatTwoPlusTwoEqualsFourAMillionTimes));

so funcParameter would be a much better name for the parameter of timeFuncRuntime function:

const timeFuncRuntime = funcParameter => {

But what does funcParameter do?

funcParameter is just a parameter with a logic name.

when we define a function, we can give a function a parameter:

const sayHello = (name) => {
   console.log(`hello, ${name}`);
}

then when we call the function, we can pass an argument:

sayHello('davsalooki')
sayHello('stetim')

in case of timeFuncRuntime, we don’t pass a string as argument, but we pass a function, so that in the body timeFuncRuntime of we can calculate the runtime of the passed function

1 Like

Here, I did in the following way by passing simply the defined function and a random value:
const checkThatTwoPlusTwoEqualsFourAMillionTimes = () => {
for(let i = 1; i <= 1000000; i++) {
if ( (2 + 2) != 4) {
console.log('Something has gone very wrong :frowning: ');
}
}
};

const addTwo = num => num + 2;

const timeFuncRuntime = funcParameter => {
let t1 = Date.now();
funcParameter();
let t2 = Date.now();
return t2 - t1;
};

// Write your code below

const time2p2 = timeFuncRuntime(checkThatTwoPlusTwoEqualsFourAMillionTimes);

const checkConsistentOutput = (time2p2, val) =>{
let firstTry = time2p2(val);
let secondTry = time2p2(val);
if(firstTry === secondTry){
return firstTry;
}else{
return ‘This function returned inconsistent results’;
}

};

checkConsistentOutput(addTwo, 2);