Doubt in FizzBUZZ

btnPrint.onclick = function() {

let start = Date.now();

let num = inpNum.value;

ulNumList.innerHTML ='';

function fizzbuzz(number) {

    if(number%3==0 && number%5==0){
        return "fizzbuzz";
    }
    if(number%3==0){
        return 'fizz';
    }
    if(number%5==0){
        return 'buzz';
    }
    if(number%3!=0 && number%5!=0){
        return number;
    }

}

for(let i=1 ; i<=num ; i++){
    let li = document.createElement('li');
    li.textContent = fizzbuzz(i);
    ulNumList.appendChild(li);
}

console.log("time taken ",Date.now()-start);

}

bhaiya , my code is running faster than Arnav Bhaiyas code.

Even though my code is not optimize like Arnav Bhaiya.

Nut in console my code is giving better resuly

I’m not able to understand why

It maybe due to different laptop. Have your tried running Arnav bhaiya’s code on your laptop and then compare?
Also, if you are using different browser, then also results may vary.

even I thought that…I did run Arnav Bhaiya’s code on my system…His code was taking twice what mine was.

Im not able to figure out how

Can you send Arnav bhaiya’s code?

btnPrint.onclick = function () {
let start = Date.now();
ulNumList.innerHTML = “”;

let c3 = 0;
let c5 = 0;
for (let i = 1; i <= inpNum.value; i++) {
let li = document.createElement(“li”);
c3++;
c5++;

let print = "";
if (c3 == 3) {
  c3 = 0;
  print += "fizz";
}
if (c5 == 5) {
  c5 = 0;
  print += "buzz";
}
if (print === "") print = i;

li.textContent = print;

ulNumList.appendChild(li);

}

console.log(“time taken”, Date.now() - start);
};

This is Arnav Bhaiyas Code.

I think it is because of usage of more variables and their frequent updation in loop.
Also, strings are being mutated in Arnav bhaiya’s code, which can degrade performance.

Either way, both are fine. The execution time difference is not very high.

1 Like

Ok bhaiya, thanks. Got it

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.