How to use console log variable value within factory functions

Hi,

How can i console.log the value of const randIndex at line 25?

Here is the code: https://gist.github.com/9ee7eb1551413e97a5939f94c545bd0c

Thanks
Jag

I would recommend to write the functions after it as comments. Probably the most simple way…

sorry didnt get that. New to how to use console log efficiently for debugging.

If i console.log outside function, i get randINdex is not defined.

Maybe simply dropping the idea of “factory function” solves your problem?
There’s no such thing. Functions have return values. Yeah, duh. This is not special.
So then your question is… how to use console.log from a function… but you probably wouldn’t ask that

Most likely you’re simply not executing your code. Surprise, nothing prints.

1 Like

@ionatan there is a complete chapter on Factory Function on codecademy.

@ionatan @stetim94 : I have tried playcode.io and vs code with quokka which provides live output when using console.log but get no output.

i tried using console.log within the function and outside and still not getting the value of i.

I believe you’re very slightly confusing factory functions with
object methods. pAequorFactory is a factory function because it can return multiple object instances. mutate() is an object method of a pAequor object. pAequorFactory(1, ['T', 'G']) is simply returning a pAequor object with those properties passed into the factory function. In order for your code inside mutate() to run you would have to call mutate() on a pAequor object.

Something quirky about console.log is it doesn’t play well with loops if there is no stopping off stage, such as a user prompt. Even then it can’t be counted upon to do anything more than push everything to a queue. We do not get real time reporting from the console (very reliably) in a loop.

Observe the behavior of this loop. It is not quirky but should give a hint to what I mean by a stopping off stage. Note that rather than logging, the output goes directly to the prompt?

while (true) {
  p = prompt(`${Math.floor(Math.random() * 100)}\nAgain?`)
  if (p === null) break
}

We only need to keep hitting Enter to get another random number. Click Cancel or press Esc and the loop ends.


The next step would be to log during this loop and observe the behavior.

while (true) {
  r = Math.floor(Math.random() * 100)
  console.log(r)
  p = prompt(`${r}\nAgain?`)
  if (p === null) break
}

We get regular logging because the program is interrupted by the prompt. Take away the interruption and logging doesn’t happen until the end of the loop. Please don’t try that with the above code. It will crash (infinite loop).

1 Like

during the objects review test: What is a Factory Function :slight_smile:

It’s not special in any way whatsoever. It’s not a language feature, it’s just an arbitrary name made up to describe something not terribly important or different.

Like I suggested earlier, you’re simply not calling your function that your console.log line is in. You’re too busy thinking it’s because it’s a “factory function” which isn’t a thing, that you miss the very basic fact that functions run when you call them.

You’re doing this:

function f() {
  console.log('beep')
}

which prints absolutely nothing until you do:

f()
1 Like

@ktsotras, thanks for the guidance and all i was missing was the mutate method

calliing the function this way pAequorFactory(1, [‘T’, ‘G’, ‘A’]).mutate(), i was able to console.log the desired variable

Took me a lot of time to understand all this but did learn a lot as well. My fav javascript debugging tool is Quokka

Thank you All