Can someone help me in developing my coding skills. I wrote code factorial. I see different logic at different places but no where I found the one that I wrote. Please tell me if there is anything worng in the below javascript code.var btn = document.querySelector('button');

btn.onclick = function(){
var total =1;
var num = Number(document.getElementById("num").value);
for(var i=0;i<num;i++){
document.getElementById('result').value = 1;
document.getElementById('result').value = total;

First: Have you tried it? Does it do what you expect?

Second: If you're gonna short circuit a process ( if (num === 0) .... ) then do it before the work starts. Why wait until after you try to do work to stop work that is almost done?

Really, there is nothing wrong with your solution - but there are other ways to do what you are trying to do. For starters - you can optimize by starting i = num, and then count backwards with i-- instead of i++. This way you can save a process on the subtraction from num.

Also, your attachment for the onlick is not very "javascript" like. Most people would do oBtn.addEventListener("click", fnPointer); But for your simple case, what you did was fine.

Good job and keep up the good work!


Start with the axiom "computer code is for humans to read and only incidentally for computers to execute" and have another look at your code.

  1. it is poorly formatted (no indentation)
  2. it is uncommented

See this code snippet for an example of code that is commented and indented for humans to read. You may know what your code does when you look at it again tomorrow but what if you look at it in 6 months or a year? What about someone else looking at it? Get into good habits right from the start so they become ingrained. Bad habits also become ingrained. They say "practice makes perfect". Unfortunately it is also true that malpractice makes malperfect.