Factorial Calculator

JS and Swift

This is two tutorials in one, it is written for both Javascript and Swift. Please do not get confused, there is one part for JS and the other for Swift.

Prerequisites

To understand this tutorial it is reccomended that you have a basic understanding of Javascript (know how to create a javascript file) or Swift. If you are doing the Swift part of this tutorial, please ensure that you have X-Code downloaded and know your way around a playground.

Introduction

To make a function that prints out the factorial of a given number is pretty easy. In this tutorial you will learn how to do it in both Javascript and Swift. If you don't know what a factorial is, it is where a number is multiplied by every number below it and itself, so for example 5! (5! is 5 factorial represented mathimatically) is 1x2x3x4x5 which is 120.

Firstly, lets think about the concept as the concept is the same in both languages. We need to make a function that accepts a paramater and then it returns the factorial of that number. To make it return the factorial, we need to make it so that the number is taken in, and every number from 1 to that number is multiplied by eachother. Lets start by setting up our loops


Javascript:

        

function factorial(n){

  var temp;
  for (var i = 1; i <= n; i = i + 1){
    // will run this loop 1 to n times.
    // in this case 5.
  }
  return temp;
}
var result = factorial(5);
console.log(result);
        
      


Swift:

        

func factorial(_ pram: Int) -> Int{
  
  var temp = 1
    for i in 1...pram{
      // will run this loop 1 to n times.
      // in this case 5.
    }
   return temp
}
var result = factorial(5)
print(result)
        
      

In the above examples we simple create a function, with a paramater which is the number of our factorial. We then pass that number into our function and then we run a loop that will loop through from 1 to our paramater. Now we want to make it so that we multiply each time the loop runs, which will give us our factorial. We can do this by setting a default value of 1, then multiplying it by i which is the loop counter.


Javascript:

        

function factorial(n){
  var temp = 1;
  for (var i = 1; i <= n; i = i + 1){
    temp = temp * i;
  }
  return temp;
}
var result = factorial(5);
console.log(result);
        
      


Swift:

        

func factorial(_ pram: Int) -> Int{

    var temp = 1
    for i in 1...pram{
      temp = temp * i
    }
   return temp
}
var result = factorial(5)
print(result)
        
      

Above we have simply made a loop that will now multiply all the numbers up to 5 (our input) together. The loop will run 5 times, if we walk through the loop you can see how it works:

  1. The loop runs once, i now equals 1, temp now equals 1.
  2. The loop runs again, i now equals 2, temp now equals 2.
  3. The loop runs again, i now equals 3, temp now equals 6.
  4. The loop runs again, i now equals 4, temp now equals 24.
  5. The loop runs again, i now equals 5, temp now equals 120.

And our program works! See how big of a number you can find the factorial of without it crashing!
If you have any questions, please post below. See the fiddle below for a working example.


Last Updated: 10/3/2018, Created 3/3/2018

Swift 4
JS 5
Tutorial Version 1