-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfunctions.js
199 lines (162 loc) · 4.23 KB
/
functions.js
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
// function
function sum(a,b){ // <- parameter
return console.log(a+b);
}
function multiply(c,d){ // <- parameter
var result=c*d; // local variable
console.log(result);
}
function showmessage(message){
console.log(message);
}
//invoke function
sum(1,2); // <- arguments
sum(); // undefined
showmessage("hey there !");
multiply(3,2);
multiply(12,7);
/*function calculate_Score(enter_subjects,enter_marks){
// var enter_subjects=parseInt(prompt("enter Number of subjects"));
//var enter_totalnumber=parseInt(prompt("enter total number of your total subjects"));
var add_subjects;
var enter_marks;
for(var i=0;i<3;i++){
var x = prompt("Enter a Value", "0");
var num1 = parseInt(x);
// enter_marks=Number(prompt("enter marks"));
add_subjects+=num1;
}
console.log(add_subjects);
return add_subjects;
}
function percentage(add_subjects,enter_totalnumber){
var perc=(add_subjects/enter_totalnumber)*100;
console.log(perc);
return perc;
}
function grade(perc){
if(perc>=85 && perc<=80){
console.log("Grade A");
}
else if(perc>=75 && perc<=70){
console.log("Grade B");
}
else if(perc>=65 && perc<=60){
console.log("Grade C");
}
else{
console.log("Fail !");
}
}
calculate_Score();
percentage();
grade();
*/
var score; // global variable
function calculate_percentage(sub1,sub2,sub3,sub4,sub5){
return (sub1+sub2+sub3+sub4+sub5)/5;
}
function grade(score){
if(score>=85 && score<=100){
console.log("Grade A");
}
else if(score>=75 && score<=84){
console.log("Grade B");
}
else if(score>=65 && score<=74){
console.log("Grade C");
}
else{
console.log("Fail !");
}
}
//global vaiables
var checkscore1=calculate_percentage(45,33,67,53,89);
var checkscore2=calculate_percentage(89,93,87,91,34);
var checkscore3=calculate_percentage(89,78,87,84,78);
console.log(checkscore1);
console.log(checkscore2);
console.log(checkscore3);
grade(checkscore1);
grade(checkscore2);
grade(checkscore3);
// function expressions manipulations
function sum1(i,j){ // <- parameter
return i+j;
}
function multiply2(g,h){ // <- parameter
return g*h
}
//multiply2(9,3); //27
console.log(sum1(5,2)-3); //4 left to right calculation
console.log(sum1(multiply2(4,5),9)); // 4*5 = 20 , 20+9=29
/*// function expressions manipulations
function sum1(i,j){ // <- parameter
return i+j;
}
function multiply2(g,h){ // <- parameter
return sum(g,h)+8 // 7+8=15
}
//multiply2(9,3); //27
console.log(sum1(5,2)-3); //4 left to right calculation
*/
// function can be made using variable , note ! use var with variable name is not necessery
var joint = function (a,b){
console.log(a+b);
return a+b;
};
joint("jf",17);
// function hoisting
var msg=energy("hoisting works ! ");
console.log(msg); // executed.
var yo=joint(4,17);
console.log(yo); // undefined
var joint = function (n,o){
//console.log(a+b);
return n+o;
};
function energy(message){
return message;
}
/*
// arguments passed by value
var num=5;
var at = (a) => {
a=7; // a recieves 5 , it remains same no effect comes in.
};
at(num); // no change occur if a is passed by value.
console.log(num);
// arguments passed by reference num , string boolean -> non primitive change occur.
var arr=[1,2,4,6];
function changeout(val){
val[2]=44; // change occur in original array despite both variables are changed but pointing the same array
}
console.log(arr[2]); // before value 4
changeout(arr);
console.log(arr[2]); // updated value 44
// recursive function calling itself again and again until terminate condition met.
var ask=parseInt(prompt("Enter a number for factorial : "));
function recur(ask){
if(ask<=1){
return 1;
}
else{
return ask*recur(ask-1); //e.g : 4*(4-1)=12 -> 12*(3-1)=24 -> 24*(2-1)=24
}
}
console.log(recur(ask)); */
// arrow functions
//function doingStuff(x) {
// console.log(x);
// }
//convert into arrow function
var doingstuff = x => {
console.log("arrow function variable passing with argmuemnt named x"+x);
}
doingstuff("yo");
let add = (x,y) =>{
console.log(x+y);
}
add(1+4);
let arr = ["apple","banana","Mango"];
arr.forEach(e => console.log(e));