PLP Lab Manual 1
PLP Lab Manual 1
PLP Lab Manual 1
[2nd Year ]
Lab Manual 1
(By: Dawit Z, MIT/2019)
Objectives:
In this manual you will learn about:
★ Defining a function
★ Bindings and scopes
★ Arrow functions
★ Higher Order Functions
1
1. JavaScript
★ JavaScript was introduced in 1995 as a way to add programs to web pages in the
Netscape Navigator browser.
★ Since then adopted by all other major graphical web browsers.
★ Made modern web applications possible - interact directly without doing a page
reload for every action.
★ A standard document was written to describe the way the JavaScript should work
so that the various pieces of software that claimed to support JavaScript were
actually talking about the same language. This is called the ECMAScript
standard, after the Ecma International organization that did the standardization.
★ There have been several versions of JavaScript.
→ ECMAScript 3 was widely supported b/n 2000 and 2010.
→ ECMAScript 6 in 2015
→ ECMAScript 8 in 2017
★ Web browsers are not the only platforms on which JavaScript is used. Some
databases, such as MongoDB and CouchDB, use JavaScript as their scripting and
query language. Several platforms for desktop and server programming, most
notably the Node.js project, provide an environment for programming JavaScript
outside of the browser.
2. Environment Set up
★ To write, test and run your JavaScript code, you need the following
→ Editor
→ Browser
★ You can write your JavaScript code using even a basic text editor such as
Notepad. However, it is good for you to get an advanced text editor that will
provide you with features such as syntax error highlighting, auto-completion, etc.
3. Visual Studio Code
★ Visual Studio Code is a lightweight but powerful source code editor which runs
on your desktop and is available for Windows, mac OS and Linux.
★ It comes with built-in support for JavaScript, TypeScript and Node.js and has a
rich ecosystem of extensions for other languages (such as C++, C#, Java, Python,
PHP, Go) and runtimes (such as .NET and Unity).
Installation
★ Download and install from https://code.visualstudio.com/
★ Install extensions, Example - Live Server, Babel JavaScript, JavaScript Code
Snippets, Prettier, …,
2
4. Functional Programming Using JavaScript
3.1. Functions
★ Functions are the bread and butter of JavaScript programming.
★ It is wrapping a piece of program in a value.
★ Gives a way to:-
→ structure larger programs
→ reduce repetition
→ associate names with subprograms
→ isolate these subprograms from each other
3.2. Defining a function
★ The definition and cal of a JavaScript function takes the following syntax:
3
★ The following example defines a function named HelloWorld(). Immediately
after the body of the function, we have called it by its name. The function will be
called immediately when the web page is loaded. The code returns Hello World.
★ However, many times we need to call functions by click of a button.
Function Arguments
4
★ We have created a function named MyFunction taking two arguments namely
arg1 and arg2. The function is to concentrate the values of these two arguments
with a text and show on an alert box.
★ We have then called the same function three times, passing different parameters
each time, after running the code, click the ok button to move through the
sequence of alert boxes.
5
3.3. Bindings and scopes
★ Each binding has a scope, which is the part of the program in which the binding is
visible. For bindings defined outside of any function or block, the scope is the
whole program - you can refer to such bindings wherever you want. These are
called global.
★ But bindings created for function parameters or declared inside a function can be
referenced only in that function, so they are known as local bindings.
★ Bindings declared with let and const are local.
★ Each scope can “look out” into the scope around it, so x is visible inside the block
in the example. The exception is when multiple bindings have the same name - in
that case, code can see only the innermost one.
★ For example, when the code inside the halve function refers to n, it is seeing its
own n, not the global n.
6
3.3. Arrow function
★ This is another notation for functions, which looks very different from the others.
★ Instead of the function keyword, it uses an arrow (=>).
[1, 2, 3, 4].map(double) // [ 2, 4, 6, 8 ]
[1, 2, 3, 4].map(function(n){
return n * 2
}) // [ 2, 4, 6, 8 ]
★ The map function is one of the many higher-order functions built into the
language.
★ Sort, reduce, filter, forEach are other examples of higher-order functions built
into the language.
★ higher -order functions allow you to write simpler and more elegant code.
★ Let’s look at the following examples.
7
Suppose we want to apply a function to a list of numbers, and then keep only
the values that are even.
8
Exercise
1. consider that % (the remainder operator) can be used to test whether a number is
even or odd by using % 2 to see whether it’s divisible by two. Here’s another way
to define whether a positive whole number is even or odd:
• Zero is even.
• One is odd.
• For any other number N, its evenness is the same as N - 2.
Define a recursive function isEven corresponding to this description. The function
should accept a single parameter (a positive, whole number) and return a Boolean.
Test it on 50 and 75. See how it behaves on -1. Why? Can you think of a way to
fix this? Enter this program and compile it. Why does it fail? How can you fix it?
2. Bean counting
You can get the Nth character, or letter, from a string by writing "string"[N]. The
returned value will be a string containing only one character (for example, "b").
The first character has position 0, which causes the last one to be found at position
string.length - 1. In other words, a two-character string has length 2, and its
characters have positions 0 and 1.
Write a function countBs that takes a string as its only argument and returns a
number that indicates how many uppercase “B” characters there are in the string.
Next, write a function called countChar that behaves like countBs, except it takes
a second argument that indicates the character that is to be counted (rather than
counting only uppercase “B” characters). Rewrite countBs to make use of this
new function.