-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFunc.js
More file actions
173 lines (169 loc) · 6.29 KB
/
Func.js
File metadata and controls
173 lines (169 loc) · 6.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
//Function Declaration / defining
//Block of code that performs a specific task , can be invked whenever needed.Function saves from redundancy means repeating
// Function Definition & Function Call or Invoke
function myFunction(msg) {
console.log(msg);
}
myFunction("I love TS"); // arguments
// Function to Calculate Sum & return
// Imp is that function parameters -> like local variable -> Block scope of function
function sumFunction(a, b) {
let s = a + b;
return s; // returns are last to right else what ever written after will definetly not run
}
let val = sumFunction(3, 4);
console.log(val);
// OPTIONAL PARAMETERS in Functions / parameterized function
function disp_details(id, name, mail_id) {
console.log("ID:", id);
console.log("Name", name);
if (mail_id != undefined) // The function prints the value of mail_id only if the argument is passed a value.
console.log("Email Id", mail_id); //If an optional parameter is not passed a value during the function call, the parameter’s value is set to undefined
}
disp_details(123, "John");
disp_details(111, "mary", "mary@xyz.com");
//ARROW FUNCTION. Compact way of writing a Function , Arrow function stores its value in a variable.
// () => {} Syntax for Arrow Function
let arrowFunction = (x, y) => {
let sum = x + y;
return sum;
};
let vals = arrowFunction(8, 10);
console.log(vals);
//Arrow -2
let prepNewMeal = (dishName) => {
console.log("Making:" + dishName);
};
prepNewMeal("Russian Salad");
//Calculate an Averge bt 3 numbers with Arrow Function
let AvgFunction = (a, b, c) => {
let avg = (a + b + c) / 3;
return avg;
};
let result = AvgFunction(40, 20, 22);
console.log(result);
//Q: Function that takes a String as an argument and returns the number of Vowels in the string
function countVowels(word) {
word = word.toLowerCase();
let count = 0;
for (let chr of word) {
if (chr === "a" ||
chr === "e" ||
chr === "i" ||
chr === "o" ||
chr === "u") {
count++;
}
}
return count;
}
const inputWord = "Apna College";
const vowelCount = countVowels(inputWord);
console.log(`Number of vowels in "${inputWord}": ${vowelCount}`);
// Create an arrow funtion to perform same task
let countvowels = (word) => {
word = word.toLowerCase();
let count = 0;
for (let chr of word) {
if (chr === "a" ||
chr === "e" ||
chr === "i" ||
chr === "o" ||
chr === "u") {
count++;
}
}
return count;
};
const inputWords = "Apna College";
const vowelsCount = countVowels(inputWord);
console.log(`Number of vowels in "${inputWord}": ${vowelCount}`);
// Callback Function: In TS a function can be passed as an arguemnts to another funtion
// for an array (only used for Arrays) forEach method is used by using call back funtion
// Interview Question: forEach is also called as Higher order function/ Method having parameters/n
// and return other function as output, where there is a call back function as paramter in another function it becomes a higher order function.
let arr = ["Islamabad", "lahore", "Karachi"];
arr.forEach((val, idx, arr) => {
console.log(val.toUpperCase(), idx, arr);
});
//Practice Question : for a given array , print squre of each value using forEach loop.
let arryNum = [1, 2, 3, 4, 5, 6];
arryNum.forEach((val) => {
console.log(val * val);
});
export {};
// Or another way
// let calSqure =(val) => {
// console.log(val * val)
// }
// arryNum.forEach(calSqure);
// function calculateAverage (num1: number, num2: number) { // this method name is Calculate averge and we have passed two parranmeters num1 and num2, or this method is beng defined with two Parameters
// return (num1+num2)/2; // when this method return value its type will be number
// }
// let avg = calculateAverage(20,50); //20 +50/2 =35
// console.log(avg);
//same method with arrow Function
// // let avg2 = (num1: number, num2: number) => (num1+num2)/2;
// let avg2 =(num1: number, num2: number) => {
// let myAvg = (num1+num2)/2;
// return myAvg;
// }
// // a Function which do not return a value
// function calculateAverage2() {
// console.log("Calculating Average");
// }
// // using thsi function in a variable to store a value
// let avg3 = calculateAverage2();
// console.log(avg3)
// let f3 = (age:number,name:String) => {
// return `Mr .${name}, your age is ${age}`;
// }
// console.log(f3 (100,"Aimon"));
// //Optional Parameters
// function optParmas(param1?:number, param2:string = "Gamma",param3:number=10){ // Optional parameter should be at aend, if all 3 parameters are optional itr will work
// console.log(`${param1}, ${param2}, ${param3}`);
// return `${param1}, ${param2}, ${param3}`;
// }
// let xyz = optParmas(10,"Alpha",20); // if we set some varibale default at the end as above
// console.log(xyz);
// let xyz2 = optParmas(10,"Beta") // if we call it this way what should print here
// console.log(xyz2); // what would print here, printing two times as once in method and once we ourself printing
// let xyz3 = optParmas(10); // where there is a no value and make that print it is rule of thum that underfined print there and first parameter can never be optional
// console.log(xyz3);
// // why first parameter can not be optional below
// // before required paramter there could not be a optional paramter this is rule of thum
// function optParams2(num1?:number , num2?:number) {
// console.log(num1);
// console.log(num2);
// }
// //optParams2();
// //optParams2(10);
// optParams2(10,20);
// //Functions - and syntax:
// // function fun_name(parameter) {
// //code
// // }
// //function declaration
// function greet(name: string) {
// console.log("Hello, " + name.toUpperCase() + "!!");
// }
// greet("John"); //function call
// //function declaration
// function Welcome(parameter: string) {
// console.log(`Welcome ${parameter}`);
// }
// Welcome("wania"); //function call
// Welcome("Class 54");
// function getFavoriteNumber(): string {
// return "Value is positive";
// }
// // console.log(getFavoriteNumber())
// getFavoriteNumber();
// //Arrow Function
// let sum1 = (x: number, y: number): number => {
// return x + y;
// };
// // console.log(sum1(2,3))
// //Arrays
// let arr = [1, 2, 3, 4, 5, 6];
// console.log(arr[0]);