Web Development Using Angular JS and MONGO
Unit 2
Array Methods :indexOf, join, lasIndexOf, toString - Array Methods : reduce, reverse, slice, some, sort - Function Definition - Function Parameters - Calling a Function - Return Statements - Nested Functions - Example Programs
Lab 4 – Functions
Web stacks introduction - LAMP and LEMP - Difference between php and java script - MEAN, MERN - Angular Environment set up – windows - Angular JS Framework - Angular JS with HTML - Angular ng directives
Lab 5 – Working with Angular js and HTML
Angular directives - Angular JS Expressions - Angular JS Applications - Angular JS Module
Angular JS Controller - Angular JS Numbers - Angular JS Strings - Angular JS Objects
Lab 6 – Work with numbers, strings and objects
References
3
Array Methods :indexOf� [forward search method]
4
Syntax: Array.indexOf(search, startIndex)
var arr = [10, 20, 30, 40, 50, 70, 10, 50];
document.write(arr.indexOf(10)); // 0
document.write(arr.indexOf(50)); // 4
document.write(arr.indexOf(70)); // 5
Array Methods :lasIndexOf�[backword search method]
5
Syntax:
Array.lastIndexOf(searchElement[, fromIndex = Array.length - 1])
var arr = [10, 20, 30, 40, 50, 70, 10, 40];
console.log(arr.lastIndexOf(10)); // 6
console.log(arr.lastIndexOf(40)); // 7
console.log(arr.lastIndexOf(100)); // -1
Convert Array to string Separated By Comma javaScript
6
Array Methods :toString
7
Syntax: array.toString();
!DOCTYPE html>
<html lang="en"> <head>
<meta charset="utf-8">
<title>JavaScript Convert Array into Comma-Separated String with toString() Method</title>
</head>
<body>
</body> </html>
<script type = "text/javascript“>
var lang = ['php','javascript','python', 'c', 'c+', 'java'];
document.write( "JavaScript - convert an array into comma-separated strings are :-" + lang.toString() );
</script>
Array Methods :join
8
Syntax: array.join(separator);
<!DOCTYPE html>
<html lang="en"> <head>
<meta charset="utf-8">
<title>JavaScript Convert Array into Comma-Separated String With join() Method</title>
</head>
<body>
</body> </html>
<script type = "text/javascript">
var lang = ['php','javascript','python', 'c', 'c+', 'java'];
document.write( "JavaScript - convert an array into comma-separated strings are :-" + lang.join(',') );
</script>
Array Methods : reduce
Initial Value current iteration value current iteration index called array
9
Syntax: reduce() method arayObject.reduce(reducer [, initialValue])
Syntax: reducer() function
function reducer(accumulator, currentValue, currentIndex, array){ }
Cont..�The initialValue argument
10
initialValue | accumulator | currentValue |
passed | accumulator = initialValue | currentValue = array[0] |
not passed | accumulator = array[0] | currentValue = array[1] |
let numbers = [1, 2, 3, 4, 5];
console.log(total); // 15
let total = numbers.reduce(
);
function (accumulator, current)
{
return accumulator + current;
}
Example 1:
Cont..�Example 2: Array of object
11
let cartItem = [
{
product: 'phone',
qty: 1,
price: 699
},
{
product: 'Screen Protector',
qty: 1,
price: 9.98
},
{
product: 'Memory Card',
qty: 2,
price: 20.99
}
];
let total = itemCart.reduce(
console.log(total);
function (acc, curr) {
return acc + curr.qty * curr.price;
}
,0);
Cont..�Example 3: Multiple arrays
12
let numbersArr = [[1, 2, 3], [4, 5, 6], [7, 8, 9]];
console.log(res); // [ 1, 2, 3, 4, 5, 6, 7, 8, 9 ]
let res = numbersArr.reduce(
);
(total, amount) => {
return total.concat(amount);
}
, [ ]
Array Methods : reverse
13
Syntax: array.reverse();
var lang = ["PHP", "Python", "Java", "C"];
lang.reverse();
// Outuput
�array: C, Java, PHP, Python
Array Methods : slice
14
Syntax: slice(begin, end);
The begin is a position where to start the extraction.
The end is a position where to end the extraction.
If begin is undefined, slice begins from the index 0.
If end is omitted, slice extracts through the end of the sequence (arr.length).
Cont..�Example 1: Clone an array
15
var nums = [1,2,3,4,5];
var res = nums.slice();
Cont..�Example 2: Copy a portion of an array
16
var lang = ['php','java','c','c#','.net', 'python'];
var res = lang.slice(0,4);
console.log(res); // ["php", "java", "c", "c#"]
Array Methods : some
17
var nums = [3, 18, 19, 20, 25];
function checkNumber(num) { return num >= 25; }
document.write(nums.some(checkNumber)); //true
Syntax: arrayObject.some(callback[, thisArg]);
Syntax: function callback(currentElement [[, currentIndex], array]){ // ...}
Array Methods : sort
1: Basic array.Sort
2: Numeric array.sort() in asc order
3: Numeric array.sort() in desc order
4: String Array Sort
5: Extract minimum using array.sort()
18
Syntax: arr.sort([compareFunction])
var lang = ["PHP", "Python", "Java", "C"];
lang.sort(); // C, Java, PHP, Python
Function
19
Function definition
20
function name(parameter1, parameter2, parameter3)
{� // code to be executed�}
function ShowMessage()
{
alert("Hello World!");
}
Function parameters
21
function ShowMessage(firstName, lastName)
{
alert("Hello " + firstName + " " + lastName);
}
The Arguments Object
22
function ShowMessage(firstName, lastName)
{
alert("Hello " + arguments[0] + " " + arguments[1]);
}
Cont..�The Arguments Object
23
function ShowMessage()
{
alert("Hello " + arguments[0] + " " + arguments[1]);
}
ShowMessage("Steve", "Jobs"); // display Hello Steve Jobs
function ShowMessage()
{
for(var i = 0; i < arguments.length; i++)
{ alert(arguments[i]);
}
}
ShowMessage("Steve", "Jobs");
Calling a Function / Function Invocation
24
Invoking a Function as a Function
25
function myFunction(a, b)
{� return a * b;�}�myFunction(10, 2); // Will return 20
Invoking a Function as a Method
26
const myObject = {� firstName:"John",� lastName: "Doe",� fullName: function () {� return this.firstName + " " + this.lastName;� }�}�myObject.fullName(); // Will return "John Doe"
Invoking a Function with a Function Constructor
27
// This is a function constructor:�function myFunction(arg1, arg2) {� this.firstName = arg1;� this.lastName = arg2;�}��// This creates a new object�const myObj = new myFunction("John", "Doe");��// This will return "John"�myObj.firstName;
call() Method
28
const person = {� fullName: function() {� return this.firstName + " " + this.lastName;� }�}�const person1 = {� firstName:"John",� lastName: "Doe"�}�const person2 = {� firstName:"Mary",� lastName: "Doe"�}��// This will return "John Doe":�person.fullName.call(person1);
The call() Method with Arguments
29
const person = {� fullName: function(city, country)
{
return this.firstName + " " + this.lastName + "," + city + "," + country;� }�}��const person1 = {� firstName:"John",� lastName: "Doe"�}��person.fullName.call(person1, "Oslo", "Norway");
Return Statements
30
function Sum(val1, val2)
{
return val1 + val2;
};
var result = Sum(10,20); // returns 30
function Multiply(val1, val2)
{
console.log( val1 * val2);
};
result = Multiply(10,20); // undefined
function multiple(x)
{
function fn(y)
{ return x * y;
}
return fn;
}
var triple = multiple(3);
triple(2); // returns 6
triple(3); // returns 9
Example: Return value From a Function
Example: Function Returning a Function
Nested Functions
31
function ShowMessage(firstName)
{
return SayHello();
}
ShowMessage("Steve");
Example: Nested Functions
function SayHello()
{
alert("Hello " + firstName);
}
Points to Remember
32
Assignment on functions
33
Unit 2
Array Methods :indexOf, join, lasIndexOf, toString - Array Methods : reduce, reverse, slice, some, sort - Function Definition - Function Parameters - Calling a Function - Return Statements - Nested Functions - Example Programs
Lab 4 – Functions
Web stacks introduction - LAMP and LEMP - Difference between php and java script - MEAN, MERN - Angular Environment set up – windows - Angular JS Framework - Angular JS with HTML - Angular ng directives
Lab 5 – Working with Angular js and HTML
Angular directives - Angular JS Expressions - Angular JS Applications - Angular JS Module
Angular JS Controller - Angular JS Numbers - Angular JS Strings - Angular JS Objects
Lab 6 – Work with numbers, strings and objects
Web stacks / web application stack
35
Web stacks / web application stack
36
LAMP, LEMP, MEAN, MERN
37
LAMP
38
The Four Layers/ Component of LAMP Stack
39
LEMP
40
Why use Nginx?
41
Why use PHP?
42
Why use MySQL?
43
Why use Linux?
44
Advantages Of LEMP
45
Disadvantages of LEMP
46
47
48
49
50