-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEvaluation2.js
274 lines (211 loc) · 6.18 KB
/
Evaluation2.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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
"use strict"
// 1. Display prime numbers 1 to 200?
function isPrime(num) {
for ( var i = 2; i < num; i++ ) {
if ( num % i === 0 ) {
return false;
}
}
return true;
}
function display(n) {
let primeNumbers = [];
for ( let i = 3; i < n; i+=2 ) {
if ( isPrime(i) ) {
primeNumbers.push(i);
}
}
console.log(primeNumbers); // use arr result on your own
}
display(200);
// 2. From two sorted arrays, how would you find the common numbers?
let list1 = [1,2,4,6,7,8]
let list2 = [2,5,6,8]
let common = []
for(let i=0;i<list1.length;i++){
for(let j=0;j<list2.length;j++){
if(list1[i]==list2[j]){
common.push(list1[i])
}
}
}
console.log(common)
// 3. Explain about function Anatomy, Anonymous function and Assigning function to a
// variable with an example?
// function update(a,b,c,d="hi";){
// a : 7
// b = []
// c = {}
// d = "hi";
// this; // context : windows or object instance
// return true;
// }
// Anonymous Functions //
(function() {
console.log('Hello');
})();
setTimeout(function () {
console.log('Execute later after 1 second')
}, 1000);
// Anonymous function used to intercept a mouse click event.
// document.addEventListener("click",function(){
// console.log("Document was clicked");
// console.log(arguments)
// })
// Assigning Functions To Variables
var print = function(){
console.log("Print something in 1 second");
console.log(arguments)
}
var clicked = function(){
console.log("Document was clicked");
console.log(arguments)
}
// We can now call these functions by their name:
print();
clicked();
// 4. Show an example ofSafegrounding function parameters?
function fun(func){
console.log(func())
}
var fArray =[];
var f = function(){}
fun(fArray) // pass array instead of function
// // Output
// c:\Users\Prakash\Desktop\javascript\Internship\new.js:2
// console.log(func())
function fun(func){
if(typeof(func) == "function"){
console.log(func())
}
}
var fArray =[];
var f = function(){}
fun(fArray)
// 5 .Explain `this` keyword with an example?
const person = {
firstName: "John",
lastName : "Doe",
id : 5566,
fullName : function() {
return this.firstName + " " + this.lastName; // this refers to the person object.
}
};
let xyz = this;
console.log(xyz == window)
// 7. Difference between map, reduce and filter methods? With an example
// map
let Arr = [3, 4, 5, 6];
let ModifiedArr = Arr.map(function(element){
return element *3;
});
console.log(ModifiedArr); // [9, 12, 15, 18]
//filters
const Numbers = [-23,-20,-17, -12, -5, 0, 1, 5, 12, 19, 20];
let Positive_array = Numbers.filter(function(value) {
return value >= 0; });
console.log(Positive_array);
// reduce
var numbers = [1,2,3,4,5];
var initialVal = 0;
let result=numbers.reduce((accu, val) => val + accu , initialVal);
console.log(result) // 15
// 8. Count Total number of zeros from 1 up to 50
const start = 0;
const end = 100;
//convert input value to strin
// check if type of value is number or not
if(typeof start === "number" && typeof end === "number" ){
let count = 0;
for(let i = start ; i <= end ; i++ ){
const numberToString = i + "" ;
count += numberToString.split("0").length - 1;
}
console.log(count);
}
var arr = [1,2,3,5,6,8];
var output =[];
var k = 0;
// 9. The following array of numbers show the missing number? ([1,2,3,5,6])
for (var i=arr[0];i<arr[arr.length-1];i++){
if(arr[k]==i){
k++
}
else{
output.push(i)}
}
if(output.length==1){
console.log(output[0])
}
else if(output.length>1){
console.log(output)
}
else{
console.log("No Missing numbers in the given array")
}
// 10. Write a program for calculating age using Date of birth? (1990)
function CurrentAge(BirthYear){
const ConvertToString = BirthYear + "";
if(typeof BirthYear === "number" && ConvertToString.indexOf(".") === -1) {
const year = new Date();
const CurrentYear = year.getFullYear();
const Age = CurrentYear - BirthYear;
console.log(Age);
}else
console.log("enter the valid value");
}
CurrentAge(2002);
let Dob = '20020413';
let Year = Number(Dob.substr(0, 4));
let Month = Number(Dob.substr(4, 2)) - 1;
let Day = Number(Dob.substr(6, 2));
let Today = new Date();
let Age = Today.getFullYear() - Year;
if (Today.getMonth() < Month || (Today.getMonth() == Month && Today.getDate() < Day)) {
Age--;
}
console.log(Age);
// 11. In the Javascript function, what are the differences between call by value and reference?
//call by value
let a = 1;
let b = a;
b = b + 2;
console.log(a); // 1
console.log(b); // 3
// cal by reference
let x = [1];
let y = x;
y.push(2);
console.log(x); // [1, 2]
console.log(y); // [1, 2]
// 12. What is Arity in Javascript? Explain with a real time example.
function f(a,b,c){}
let arity = f.length;
console.log(arity)
// 13. What is Currying in Javascript? Explain with a real time example.
// Currying
let planet = function(a){
return function(b){
return `Favorite planets are ${a} and ${b} `
};
};
// let favplanet = planet("jupitar");
// console.log(favplanet("Earth"));
// console.log(favplanet("Mars"));
// console.log(favplanet("Venus"));
// Call the curried function with two argument
console.log(planet("Pluto")("Moon"));
let planets = (c) => (d) => `Favorite planets are ${c} and ${d} `
console.log(planets("venus") ("mars"));
// 16. List the differences between named function and assigning functions to variable with
// examples
// function declaration
hoisted(); // logs "foo"
function hoisted() {
console.log("foo");
}
// Function expression
notHoisted(); // TypeError: notHoisted is not a function
var notHoisted = function() {
console.log("bar");
};