📒
SDV503
  • SDV503
  • Command Prompt
    • Windows Command Prompt
    • Mac OS Terminal
  • GIT & GITHub
    • GitHub
    • Git
      • Git Workflow
        • Forking Workflow
  • README
    • How to write a readme.md file?
    • Write a better README.me
    • Generate a README for GitHub Repo Quickly
  • Code-comments
    • Clean Comments
    • Writing Comments in a Good way
  • Pair Coding
    • What is Pair Coding?
    • Pair Programming Experience
  • Programming?
    • What is Programming?
    • What Is A Programming Paradigm?
    • JavaScript Programming Paradigms
  • Number Systems
    • Decimal and Binary numbers
  • JavaSCript
    • Introduction To JavaScript
      • The JavaScript Engine
  • JS Call Stack
    • JavaScript call stack
    • JavaScript & Memory
      • Memory Leaks in JavaScript
    • Execution Context and Execution Stack in Javascript
      • Javascript Execution Context and Hoisting
  • JavaScript Variables
    • Introduction to JS Data Types
    • Primitive and Non-Primitive
    • Operator precedence and associativity
      • JS Operators Part One
      • JS Operators Part Two
      • JS Operators Part Three
    • Clean Code for JS Variables
  • JavaScript Scopes
    • Scope (Chain) Visualized
  • Javascript  — this keyword
  • JavaScript Data Types
    • More JavaScript Data Types
  • JavaScript Expressions and Statements
  • if/else, switch and ternary operator
    • If/Else statement
  • Switch Statement
  • Ternary Operator
  • JavaScript Loops
    • Loops in JavaScript
      • Trouble With Loops
  • Objects
    • JavaScript Objects
      • Prototypal Inheritance Visualized
      • JavaScript Number Object
      • JavaScript String Object
  • Functions
    • JavaScript Function Part One
    • JavaScript Function Part Two
    • Immediately Invoked Function Expressions ~ IIFE
    • JS => Arrow Functions
    • JavaScript Callback
    • Hoisting in JavaScript
      • Hoisting Visualized
    • Recursion Functions
    • Curry and Function Composition
  • JavaScript Features
    • JSpread Operator
    • JavaScript Built-in Functions & Objects
  • Data Structures&Algorithms
    • JavaScript’s Data Types
    • Data Structures in JavaScript
      • Introduction to Big O Notation
      • Big O Notation in Javascript
      • Linked Lists
        • Linked Lists — 2
      • Hash Tables
      • Stack & Queue
  • TLDR
    • Single quotes (‘ ’) and double quotes (“ ”) in JavaScript
  • ES6
    • Generators and Iterators
    • Javascript Classes
    • JavaScript closures
    • JavaScript Promises & Async/Await
      • Event Loop Visualized
  • C#
    • What does C#? (C Sharp)
    • C# vs JavaScript
    • What Is The Difference Between C#, .NET, ASP.NET, Microsoft.NET, and Visual Studio?
    • What is the .NET Framework?
    • Methods and Properties of Console Class in C#
    • Datatypes in C#
    • C# Code Comments
    • The if statement
    • The switch statement
    • Loops
    • Comparison operators
    • Addition assignment operators
    • The String Interpolation Operator
    • Arrays
    • Lists
    • Dictionaries
Powered by GitBook
On this page
  • Let’s learn how to generalize functions in JS!
  • Generalizing functions

Was this helpful?

  1. Functions

JavaScript Function Part Two

Let’s learn how to generalize functions in JS!

Let’s quickly revise what we’ve learned:

Javascript functions are like machines.

We can define them — i.e “build the machine”.

We can call them — i.e “run the machine”.

We can define what inputs (“raw materials”) the machine will take — i.e we specify function parameters.

We can give the actual inputs to the machine when we run it — i.e we call our function, and we pass in arguments it will use.

In part 1, we’ve seen an example of a Bottle Capper machine and then we wrote a JavaScript function to imitate that behavior.

When we built the Bottle capper machine in the form of the bottleCapper function, we gave it two parameters: a bottle and a cap.

function bottleCapper(bottle, cap) {
  return bottle + cap;
}

Now we realize that we want to build another machine. This one will staple two pieces of paper together, so we’ll call it paperStapler.

function paperStapler(firstPiece, secondPiece) {
  return firstPiece + secondPiece
}

And now we can run it too, just like this:

paperStapler(“Page 1”, “Page 2”)

Great! We’ve just built and ran our new machine!

Soon enough, if we keep going like this, we might have a full warehouse of those things!

We just think of a simple machine, and poof — we build it, just like that.

Things can’t possibly get better than this, right?

Or can they?

Truth is, if we compare our bottleCapper and our paperStapler , we’ll see they’re doing almost the exact same thing.

Even their structure is almost the same. Generally, we could look at it like this:

function machineName(something1, something2) {
  return something1 + something2
}

Taking things even further, let’s change a rather non-descript machineName function name and call it concatenateArgs.

“Concatenate” is a fancy way of saying “put them together”, and “Args” is just an abbreviation of “Arguments”.

We can make our function name even shorter, and call it concatArgs.

Instead of something1 and something2, let’s give it a more generic a and b.

Now we have our updated function definition:

function concatArgs(a, b) {
  return a + b
}

We’ve just built a magical machine! We no longer have to specify if it’s a Bottle capper or a Paper stapler — whatever two things we give it, it will put them together.

Thus, instead of filling up our warehouse with a bunch of machines that generally do the same thing, we’ve just built a single one that’s gonna do all the work. Pretty cool stuff!

Generalizing functions

Hopefully you’ve caught me writing the word “generally” in the previous section twice. It was a hint at the formal name for what we did in the previous section: We took the bottleCapper and the paperStapler functions and we generalized them into concatArgs.

Generalizing makes our functions applicable to more situations.

Let’s look at another example to see this in practice.

Let’s say we want to allow a user to give us a number, and we’ll tell our user if that number is divisible by 2.

Of course, this is a trivial example, but our focus here is on function generalization; our focus is not on building complex functions.

We’ll keep things even simpler and use ES5 syntax:

var divisibleByTwo = function() {
  var num = Number(prompt("Enter a number"))
  var testResult = num % 2;
  if (testResult == 0) {
    alert ("This number IS divisible by 2");
  } 
  if (testResult != 0) {
    alert ("This number ISN'T divisible by 2");
  }
}

In the above function, we’re keeping things as simple as possible:

  • On line 1, we start to define our function

  • On line 2, we prompt the user for an answer. Since that answer is a string, we convert it to a number using the native Number() function

  • On line 3, we declare the testResult variable; we also store the result of num % 2 inside of it. The % is the modulus operator, and returns the remainder of division. If our number is divisible by 2, there will be no remainder — i.e the remainder of such division is zero.

  • On lines 4, 5, and 6, we test if the value stored in testResult is indeed a zero. If it is, we’ll alert the message: This number IS divisible by 2.

  • On lines 7, 8, and 9, we test if the value stored in testResult is not a zero. This means that our division has a remainder, such as if we divided 3 with 2, or 5 with 2, or 7 with 2, etc. If this if statement’s condition is satisfied, we’ll alert the message: This number ISN’T divisible by 2.

Let’s now imagine that we need to check if user input is divisible by 3.

We could write a similar function to the one we just saw:

var divisibleByThree = function() {
  var num = Number(prompt("Enter a number"))
  var testResult = num % 3;
  if (testResult == 0) {
    alert ("This number IS divisible by 3");
  } 
  if (testResult != 0) {
    alert ("This number ISN'T divisible by 3");
  }
}

This function, just like the previous one, works perfectly.

But how do we make a function that will check if a number is divisible by any other number, without remainder? Do we write an infinite number of functions, for each specific number?

Of course not. That would be silly.

Instead, we write a generalized divisibleByX function, like this:

var divisibleByX = function() {
  var testedNumber = Number(prompt("Enter the num to be tested"));
  var x = Number(prompt("Enter the num that tests"));
  var testResult = testedNumber % x;
  if (testResult == 0) {
    alert ("This number IS divisible by " + x);
  } 
  if (testResult != 0) {
    alert ("This number ISN'T divisible by " + x);
  }
}

Now we can run our function, and we’ll be asked two questions:

  1. Enter the num to be tested — I’ve typed in 15.

  2. Enter the num that tests — I’ve typed in 5.

Once we give these two answers, we’ll get back the following message:

This number IS divisible by 5.

If we give a different answer, such as 16 and 5, the message will be:

This number ISN’T divisible by 5.

PreviousJavaScript Function Part OneNextImmediately Invoked Function Expressions ~ IIFE

Last updated 5 years ago

Was this helpful?