{"id":19,"date":"2019-02-19T18:30:09","date_gmt":"2019-02-19T18:30:09","guid":{"rendered":"https:\/\/code.jamesash.com\/?p=19"},"modified":"2019-02-19T18:30:09","modified_gmt":"2019-02-19T18:30:09","slug":"factorial-functions","status":"publish","type":"post","link":"https:\/\/code.jamesash.com\/?p=19","title":{"rendered":"Factorial Functions"},"content":{"rendered":"\n<p>I went to CoderByte to see what kind of challenges are out there to stay fresh with. There was one about calculating factorials. It goes like this:<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p>Have the function\u00a0FirstFactorial(<strong>num<\/strong>)\u00a0take the\u00a0<strong>num<\/strong>\u00a0parameter being passed and return the factorial of it. For example: if\u00a0<strong>num<\/strong>\u00a0= 4, then your program should return\u00a0<strong>(4 * 3 * 2 * 1)<\/strong>\u00a0= 24. For the test cases, the range will be between 1 and 18 and the input will always be an integer.\u00a0<\/p><\/blockquote>\n\n\n\n<p>This one was pretty easy. I just set up a loop and ran through the numbers.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">function FirstFactorial(num) { <br> var f = 1;<br> for (var i = 1; i &lt;= num; i++) {<br>         f = i * f;<br>     }<br>     return f;<br> }<\/pre>\n\n\n\n<p>After I was done, I saw that the code hint was to use a recursive function. Admittedly, that would be better looking code. It looks something like this:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">function FirstFactorial(num) { <br> if (num === 0 || num === 1) {<br>      return 1;<br>  } else {<br>      return num * FirstFactorial(num - 1);<br>  }<br> }<\/pre>\n\n\n\n<p>Both functions get the job done.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I went to CoderByte to see what kind of challenges are out there to stay fresh with. There was one about calculating factorials. It goes like this: Have the function\u00a0FirstFactorial(num)\u00a0take the\u00a0num\u00a0parameter being passed and return the factorial of it. For example: if\u00a0num\u00a0= 4, then your program should return\u00a0(4 * 3 * 2 * 1)\u00a0= 24. &hellip; <a href=\"https:\/\/code.jamesash.com\/?p=19\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Factorial Functions<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-19","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/code.jamesash.com\/index.php?rest_route=\/wp\/v2\/posts\/19","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/code.jamesash.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/code.jamesash.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/code.jamesash.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/code.jamesash.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=19"}],"version-history":[{"count":1,"href":"https:\/\/code.jamesash.com\/index.php?rest_route=\/wp\/v2\/posts\/19\/revisions"}],"predecessor-version":[{"id":21,"href":"https:\/\/code.jamesash.com\/index.php?rest_route=\/wp\/v2\/posts\/19\/revisions\/21"}],"wp:attachment":[{"href":"https:\/\/code.jamesash.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=19"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/code.jamesash.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=19"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/code.jamesash.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=19"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}