-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path6-callBacktask.js
More file actions
111 lines (101 loc) · 3.15 KB
/
6-callBacktask.js
File metadata and controls
111 lines (101 loc) · 3.15 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
// ### Challenge `processLength`
// * Implement a higher-order function called `processLength`.
// * It takes two arguments:
// * @param list an array with elements of any type.
// * @param callback function that takes a number as its argument.
// * @returns the result of invoking `callback` passing the LENGTH of `list`.
var a=[1,"rahul",3,3,6545,"gagan"];
function processLength(aray,res)
{
var c=aray.length;
res(c);
}
function callback(num)
{
console.log("Length of the array is: "+num);
}
processLength(a,callback);
// * ### Challenge `processLastItem`
// * @instructions
// * Implement a higher-order function called `processLastItem`.
// * It takes two arguments:
// * @param stringList array of strings.
// * @param callback function that takes a string as its argument.
// * @returns the result of invoking `callback` with the LAST element in `stringList`.
var a=["rahul","deepak","negi","gagan"];
function processLastItem(aray,res)
{
var c=aray.length;
var b=aray[c-1];
res(b);
}
function callback(str)
{
console.log("Last element of the array is: "+str);
}
processLastItem(a,callback);
// * ### Challenge `processSum`
// * @instructions
// * Implement a higher-order function called `processSum`.
// * It takes two arguments:
// * @param numberList array of numbers.
// * @param callback function that takes a number as its argument.
// * @returns the result of invoking `callback` passing the SUM of all elements in `numberList`.
var numberList=[32,323,24,5,65];
function processSum(aray,res)
{
var d=0;
for(var i=0;i<aray.length;i++)
{
d+=aray[i];
}
res(d);
}
function callback(sum)
{
console.log("Sum of all the elements of the array is: "+sum);
}
processSum(numberList,callback);
// @instructions
// * Implement a higher-order function called `processProduct`.
// * It takes three arguments:
// * @param num1 a number.
// * @param num2 a number.
// * @param callback function that takes a number as its argument.
// * @returns the result of invoking `callback` passing the PRODUCT of `num1` and `num2`.
function processProduct(num1,num2,res)
{
var d=num1*num2;
res(d);
}
function callback(product)
{
console.log("Product of two numbers is: "+product);
}
processProduct(25,25,callback);
// @instructions
// * Implement a higher-order function called `processContains`.
// * It takes three arguments:
// * @param item of any kind.
// * @param list array of elements of any kind.
// * @param callback function that takes a boolean as its argument.
// * @returns the result of invoking `callback` passing true if `item` exists in `list`, false otherwise.
var List=[32,323,"rahul","gagan",65,"abhinav"];
function processContains(anyItem,aray,boolres)
{
for(var i=0;i<aray.length;i++)
{
if(anyItem===aray[i])
{ var g=true; break; }
g=false;
}
boolres(g);
}
function callback(bool)
{
if(bool==true)
console.log("Item exists in the array");
else
console.log("Item doesn't exist in the array");
}
processContains('rahul',List,callback);