-
Notifications
You must be signed in to change notification settings - Fork 0
/
8.1_Practice_set_1.html
216 lines (173 loc) · 5.24 KB
/
8.1_Practice_set_1.html
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
<!DOCTYPE html>
<html>
<head>
<script>
// Code 1:
let x= {}, y = {name:"Ronny"},z = {name:"John"};
x[y] = {name:"Vivek"};
x[z] = {name:"Akki"};
console.log(x[y]); // {name: "Akki"} overwritten the same prop
// Code 2:
function runFunc(){
console.log("1" + 1); //1
console.log("A" - 1); //NaN
console.log(2 + "-2" + "2"); //2-22
console.log("Hello" - "World" + 78); //NaN
console.log("Hello"+ "78"); //Hello 78
}
runFunc();
// Code 3:
let a = 0;
let b = false;
console.log((a == b)); //true
console.log((a === b)); //false
// Code 4
x = 23;
(function(){
var x = 43;
(function random(){
x++; // var x;
console.log(x); //NaN random() has functional scope since x is declared and hoisted in functional scope
var x = 21; //initialization
})();
})();
//Code 5
let hero = {
powerLevel: 99,
getPower(){
return this.powerLevel;
}
}
let getPower = hero.getPower;
let hero2 = {powerLevel:42};
console.log(getPower()); // undefined
console.log(getPower.apply(hero2)); // 42
// Code 6
a = function(){
console.log(this); //global/window object
b = {
func1: function(){
console.log(this); // func1: f
}
}
const c = {
func2: ()=>{
console.log(this); //global/window object
}
}
b.func1();
c.func2();
}
a();
// Code 7
b = {
name:"Vivek",
f: function(){
var self = this;
console.log(this.name); //Vivek
(function(){
console.log(this.name); //undefined
console.log(self.name); //Vivek
})();
}
}
b.f();
// Code 8
(function(a){
return (function(){
console.log(a); //45
a = 23;
})()
})(45);
// Code 9
// Each time bigFunc is called, an array of size 700 is being created,
// Modify the code so that we don't create the same array again and again
function bigFunc(element){
let newArray = new Array(700).fill('♥');
return (element) => newArray[element];
}
let getElement = bigFunc();
console.log(getElement(599)); // gets the 599 value
console.log(getElement(670)); // gets the 670 value
// Code 10
// The following code outputs 2 and 2 after waiting for one second
// Modify the code to output 0 and 1 after one second.
function randomFunc(){
for(let i = 0; i < 2; i++){
setTimeout(()=> console.log(i),1000);
}
}
randomFunc();
function randomFunc(){
for(var i = 0; i < 2; i++){
(function(i){
setTimeout(()=> console.log(i),1000);
})(i);
}
}
randomFunc();
// Code 11: 6. Write a function that performs binary search on a sorted array
function binarySearch(array,value,startPos,endPos)
{
if(startPos > endPos) return 'not found';
else
{
let middleIndex = Math.floor(startPos + endPos)/2;
if(array[middleIndex] === value) return middleIndex;
else if(array[middleIndex] > value) return binarySearch(array,value,startPos,middleIndex-1);
else return binarySearch(array,value,middleIndex+1,endPos);
}
}
console.log(binarySearch([5,7,9,11],5,1,2));
/*Code 12: Implement a function that returns an updated array with r right rotations on an array of integers a .
Example:
Given the following array: [2,3,4,5,7]
Perform 3 right rotations:
First rotation : [7,2,3,4,5] , Second rotation : [5,7,2,3,4] and, Third rotation: [4,5,7,2,3]
return [4,5,7,2,3]
*/
function rotateRight(arr,rotations){
if(rotations == 0) return arr;
else{
for(let i = 0; i < rotations;i++){
let element = arr.pop();
arr.unshift(element);
}
return arr;
}
}
console.log(rotateRight([2, 3, 4, 5, 7], 3)); // Return [4,5,7,2,3]
console.log(rotateRight([44, 1, 22, 111], 5)); // Returns [111,44,1,22]
// Code 13: Write the code if 2 strings are anagrams then return true
var firsWord = 'Anam', secondWord = 'Aman';
console.log(isAnagram(firsWord,secondWord)); // false
function isAnagram(wordOne, wordTwo)
{
var a = wordOne.toLowerCase();
var b = wordTwo.toLowerCase();
//split, sort, combine, compare
a = a.split("").sort().join("");
b = b.split("").sort().join("");
return a == b;
}
// Code 14: find vowels
const findVowels = str => {
const vowels = ['a','e','i','o','u'];
let count = 0;
for( let char of str.toLowerCase())
{
if(vowels.includes(char))
count++;
}
}
// Code 15: what is the output?
b = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
for (let i = 0; i < 10; i++) {
setTimeout(() => console.log(b[i]), 1000);
}
for (var i = 0; i < 10; i++) {
console.log(i);
setTimeout(() => console.log(b[i]), 1000);
}
</script>
</html>