math - factorial in javascript. Doesn't work -
here script, don't understand why doesn't work...
function firstfactorial(num) { var lower = num - 1; var qq = 0; while (num > 0) { var qq === num * lower; num--; lower--; } num === qq; return num; } firstfactorial(num);
you have quite few issues function, see comments.
function firstfactorial(num) { var lower = num - 1; var qq = 0; while (num > 0) { // here shadowing "qq" variable in outer scope // delete "var" keyword fix // === identity operator, think wanted assignment (=) // line becomes: qq = num * lower; var qq === num * lower; num--; lower--; } // assuming === supposed = again // can rewrite just: return qq; num === qq; return num; } firstfactorial(num); even these syntactical fixes, logic calculating factorial not correct.
edit: working examples (because bored) http://jsfiddle.net/gu2cz/
Comments
Post a Comment