-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbasics_5_highlevelfunction.js
97 lines (83 loc) · 1.51 KB
/
basics_5_highlevelfunction.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
console.log(sum(range(1,50)));
var a=0;
var i=0;
while(i <= 10)
{
a +=i;
i++;
}
console.log("the sum of the following is \n " + a);
var sum=0,range,i;
i = prompt("please enter the start of the range");
console.log(i);
if(i == NaN){
alert("you have entered a invalid number");
}
else
{
range= prompt("please enter the end of the range");
console.log(range);
}
var a=parseInt(i);
var b=parseInt(range);
while(a<b)
{
sum +=a;
a++;
console.log("the step by step sum is \t" + sum);
}
// THE TRADITIONAL WAY
var arr=[1,2,3]
for(var i=0;i<arr.length;i++){
var a=arr[i];
console.log("the array element \t"+ a);
}
console.log(arr);
//HIGH ORDER FUNCTIONS
function forEach(a,task){
for( var i=0;i<a.length;i++)
task(a[i]);
}
forEach(["1","2","3"],console.log);
// USING FUNCTION INSTEAD OF ACTION
var a=[1,2,3,4,5,6],total=0;
forEach (a , function(total) {
total +=a;
});
console.log(total);
function gT(n){
return function(m){
return m>n;
};
}
var gT5=gT(5);
console.log(gT5(11));
// functions that change other functions
function noi(f){
return function(arg){
console.log("calling with",arg);
var val=f(arg);
console.log("called with",arg,"-got",val);
return val;
};
}
noi(Boolean)(1);
var a=JSON.stringify({
"name": "rama",
"sex": "m",
"born":"1958",
"father":"das"
},{
"name": "shama",
"sex": "f",
"born":"1998",
"father":"shiv sunder"
},{
"name": "bajrangi",
"sex": "m",
"born":"1985",
"father":"anjanaa"
});
console.log(a);
console.log(JSON.parse(a).born);
//to be continued