Fidel Sebastian
Fidel Sebastian

Reputation: 33

Can't figure out why undefined shows up between two desired results

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

const addOneToOne = () => 1 + 1;
timeFuncRuntime(addOneToOne());
console.log(timeFuncRuntime());

I can't figure out why undefined shows up between 2 and 0 (in the log when run). It runs fine if I remove the parenthesis attached to the functions in the last two lines. However, as soon as I add parenthesis to timeFuncRuntime in the last line, undefined shows up. I tried all combinations. I'm starting out with JavaScript and am new to coding but I am a perfectionist at understanding the least of things as it hampers my concentration and confidence going forward.

Upvotes: 3

Views: 104

Answers (5)

Heretic Monkey
Heretic Monkey

Reputation: 12124

I think your intent was to create a function that times how long it takes for a function to run. If that's the case, here's an answer that will help solve that problem, while helping to understand where you went wrong.

const timeFuncRuntime = funcParameter => {
   console.log(funcParameter); // this will log whatever was passed to the function
   let t1 = Date.now();
   funcParameter;              // this does nothing
   if (typeof funcParameter !== 'function') {
                               // this makes sure the parameter is a function and returns a string if not.
     return 'Invalid test, parameter was not a function';
   }
   funcParameter();            // this executes the function
   let t2 = Date.now();
   return t2 - t1;             // this returns the number of milliseconds as the reusult
}

const addOneToOne = () => 1 + 1;
timeFuncRuntime(addOneToOne());  // This calls addOneToOne() and passes the result to timeFuncRuntime (i.e., 2);
timeFuncRuntime(addOneToOne);    // This passes the addOneToOne function to timeFuncRuntime
console.log(timeFuncRuntime());  // This passes undefined to timeFuncRuntime and will now report that a function was not passed.
console.log(timeFuncRuntime(addOneToOne)); // This will output the number of milliseconds it took to run addOneToOne.

Upvotes: 0

smwhr
smwhr

Reputation: 683

First logline comes from line 4 called from line 10, it displays funcParameter, which is addOneToOne() which is 2

Second logline comes from line 4 again, called from line 11 this time, it displays funcParameter, which is nothing, thus print "undefined"

Third logline is the console.log on line 11 itself, displaying the return value of timeFuncRuntime, thus the difference between t1 and t2 (given that this function doesn't do much, it executes really quickly.

Are you trying to make a function that computes the runtime of another ? You could change your code to this :

const timeFuncRuntime = funcParameter => {
   let t1 = Date.now();
   var result = funcParameter();
   let t2 = Date.now();
   return {result: result, time: t2 - t1};
}

const addOneToOne = () => 1 + 1;
var elapsed = timeFuncRuntime(addOneToOne);
console.log(elapsed.result + " in " + elapsed.time + "ms");

Upvotes: 0

3limin4t0r
3limin4t0r

Reputation: 21150

Let me remove some noise for you that hopefully clears some things up.

const timeFuncRuntime = funcParameter => {
   console.log(funcParameter);
   return "foo";
}

timeFuncRuntime("bar");
console.log(timeFuncRuntime());

So what happens in the above? Why does the console print out bar, undefined then foo?

Let's have a look at this section by section.

const timeFuncRuntime = funcParameter => {
   console.log(funcParameter);
   return "foo";
}

This first piece of code defines a function and saves it in the constant timeFuncRunTime. This function accepts one parameter funcParameter. When called it will log this parameter and return "foo";

timeFuncRuntime("bar");

This calls the timeFuncRuntime function with funcParameter set to "bar". This will log the first result bar. The return value is ignored (not saved in a variable nor passed on to another function).

console.log(timeFuncRuntime());
// call without parameters ^

This line is the trouble maker. You want to log the timeFuncRuntime() return value (which is "foo"). However the function has to be called first to get this value. This time you call it with no arguments. This means that funcParameter will be undefined. You then log this parameter, resulting in the undefined in the console output. This is followed by a log of the return value foo.


If the intent was to log the return value after the function was called you will have to save the return value inside a variable, or pass it directly to console.log.

const timeFuncRuntime = funcParameter => {
   console.log(funcParameter);
   return "foo";
}

console.log("save the return value inside a variable");
const returnValue = timeFuncRuntime("bar");
console.log(returnValue); // <- log the variable

console.log("pass the return value to `console.log` directly");
console.log(timeFuncRuntime("bar"));

Upvotes: 2

ChrisR
ChrisR

Reputation: 4008

If you check below, I just added a string to the console.log in order to see from which console.log the output comes.

First one logs the parameter you gave to timeFuncRuntime, which is the return of addOneToOne, meaning 2.

Second one logs the parameter you gave to timeFuncRuntime, again but this time there's nothing, so undefined.

Third one log the return of the function timeFuncRuntime, which is the difference between t2 and t1. Which is between 0 and 2 ms depending on many factors.

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

const addOneToOne = () => 1 + 1;
timeFuncRuntime(
  addOneToOne()
); // first log
console.log(
  'timeFuncRuntime()', 
  timeFuncRuntime() // second log
); // third log

Upvotes: 0

Maimas2
Maimas2

Reputation: 961

After toying around, I cam upon 2 problems:

  1. Problem: 3 values are being printed. I am assuming this is a problem. If not, oops, you can move on to the next one. But you are logging values inthe function, but then you log the return value also. You must take away the 2nd Console.log
  2. It is printing undefined. the reason is the second time you call the function, you pass no arguments. The parameter funcParameter then defaults to undefined, and is printed.
I have fixed both problems and put the finished result below.

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

var varToPassIntoFunction = 0;

const addOneToOne = () => 1 + 1;
timeFuncRuntime(addOneToOne());
timeFuncRuntime(varToPassIntoFunction);

If you change varToPassIntoFunction, you will see that the logged variable changes too.

Upvotes: 0

Related Questions