Home > Enterprise >  I have a Javascript code that is not running in the console in Chrome
I have a Javascript code that is not running in the console in Chrome

Time:01-29

It is showing "Undefined" when I call the function. Here is the code

let output =[];
let count = 1;

function fizzBuzz() {


while(count<=100) {
    
    if(count % 3 === 0 && count % 5 === 0){
        output.push("FizzBuzz");
    }else if (count % 3 === 0){
        output.push("Fizz");
    }else if (count % 5 === 0){
        output.push("Buzz");
    }else{
        output.push(count);
    }
    
count  ;

    }

}

I am expecting it to output an array of numbers up to 100 but inserting "Fizz" for multiples of 3, "Buzz" for multiples of 5, and "FizzBuzz" for multiples of both 3 and 5.

CodePudding user response:

You have the following code

let output =[]; 
let count = 1;

function fizzBuzz() {
    while(count<=100) {
        if(count % 3 === 0 && count % 5 === 0){
            output.push("FizzBuzz");
        }else if (count % 3 === 0){
            output.push("Fizz");
        }else if (count % 5 === 0){
            output.push("Buzz");
        }else{
            output.push(count);
        }  
        count  ;
    }
}

which you are executing by calling fizzBuzz(). You're getting undefined because when you call fizzBuzz() in the console you're seeing the return value of the execution, which in your code doesn't ever return anything, so we get undefined.

You have two options.

  1. Return output at the end of your function, outside of the while loop. In this case, it'd be best to also move your output and count variables inside, like so
function fizzBuzz() {
    let output =[]; 
    let count = 1;

    while(count<=100) {
        if(count % 3 === 0 && count % 5 === 0){
            output.push("FizzBuzz");
        }else if (count % 3 === 0){
            output.push("Fizz");
        }else if (count % 5 === 0){
            output.push("Buzz");
        }else{
            output.push(count);
        }  
        count  ;
    }

    return output;
}

We move the output and count declarations inside so that every time we call fizzBuzz() we start from scratch. If we kept them outside, then if you called fizzBuzz() twice, we'd see the results from the first call in the results from the second call as well.

  1. Your second option is to call fizzBuzz() with how your code currently is, and then print the value of output. I think you're just making a simple mistake and don't realize that your fizzBuzz doesn't return anything.
fizzBuzz();
console.log(output);

or, if you're executing this in your browsers developer console then

fizzBuzz()
output

CodePudding user response:

You are missing a return statement for the output

let output =[];
let count = 1;

function fizzBuzz() {


while(count<=100) {
    
    if(count % 3 === 0 && count % 5 === 0){
        output.push("FizzBuzz");
    }else if (count % 3 === 0){
        output.push("Fizz");
    }else if (count % 5 === 0){
        output.push("Buzz");
    }else{
        output.push(count);
    }
    
count  ;

    }
return output;
}

console.log(fizzBuzz());

CodePudding user response:

If you want run your code without getting undefined then you can use var instead of using 'let'. Due to the variable block scope you are getting undefined. Just replace let with var.

Then return output variable in your function body. use console.log(your function name)

  • Related