0

So I was trying to write a JS programme when I found out that when I call the function in the js file and run it, it gives me a different output than the output I get when I don't call the function in the js and instead run it in the console.

So I have put my code for the different scenarios below:

  1. When I call my function inside js file itself and then run it.
var output=[];
var count=1;

function fizzBuzz(){
   
   output.push(count);
   count=count+1;
   
  console.log(output);
}

fizzBuzz();
  1. When I call the function in the console.
var output=[];
var count=1;

function fizzBuzz(){
   
   output.push(count);
   count=count+1;
   
  console.log(output);
}

Outputs: 1) Every time I run it, it gives me the same output i.e; [1].

    2)Calling for the 1st time, output= [1]
                  2nd time, output=[1, 2]
                  3rd time, output= [1, 2, 3]
                and goes on.
  • I am stupid or are the looking exactly the same? What is your question, how can we help you? Welcome to StackOverflow. Please complete the [Tour](https://stackoverflow.com/tour) and read ["How to Ask"](https://stackoverflow.com/help/how-to-ask). If possible you should provide a [Minimal, Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example) – Marc Jul 15 '21 at 16:59
  • Please share the output you obtain – mcsoini Jul 15 '21 at 17:00
  • @mcsoini I have added the outputs for the two scenarios. – Manuj Hazarika Jul 15 '21 at 17:13
  • What do you mean by "call your function in JS"? If that's a *file* then the count will be initialized every time the file is run. It's not at all clear what you're specifically doing. – Dave Newton Jul 15 '21 at 17:19

0 Answers0