Skip to content

Commit df96b55

Browse files
committed
small type correction
1 parent 353a293 commit df96b55

File tree

3 files changed

+27
-36
lines changed

3 files changed

+27
-36
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,34 @@
11
// Write a function to list Down All Prototype Properties of an Javascript Object
22

3-
printPrototypeProperties = obj => {
4-
var result = [];
3+
printPrototypeProperties = (obj) => {
4+
var result = []
55

6-
let objProp = Object.getOwnPropertyNames(obj)
7-
console.log(objProp)
6+
let objProp = Object.getOwnPropertyNames(obj)
7+
console.log(objProp)
88

9-
// console.log(Object.keys(obj))
9+
// console.log(Object.keys(obj))
1010

11-
for (let i of objProp) {
12-
13-
if (result.indexOf(i) === -1 ) {
14-
result.push(i);
15-
}
11+
for (let i of objProp) {
12+
if (result.indexOf(i) === -1) {
13+
result.push(i)
1614
}
17-
return result;
1815
}
19-
16+
return result
17+
}
2018

2119
let myObj = {
22-
"a": 1,
23-
"b": 2,
24-
"c": 3
20+
a: 1,
21+
b: 2,
22+
c: 3,
2523
}
2624

27-
console.log(printPrototypeProperties(myObj));
28-
25+
console.log(printPrototypeProperties(myObj))
2926

3027
/* Object.getOwnPropertyNames vs Object.keys
3128
3229
https://stackoverflow.com/questions/22658488/object-getownpropertynames-vs-object-keys
3330
3431
35-
203
36-
down vote
37-
accepted
3832
There is a little difference. Object.getOwnPropertyNames(a) returns all own properties of the object a. Object.keys(a) returns all enumerable own properties. It means that if you define your object properties without making some of them enumerable: false these two methods will give you the same result.
3933
4034
It's easy to test:
@@ -53,4 +47,4 @@ If you define a property without providing property attributes descriptor (meani
5347
a.test = 21;
5448
then such property becomes an enumerable automatically and both methods produce the same array.
5549
56-
*/
50+
*/

Javascript/js-basics/Closure/closure-use-case-create-private-variable.js

+3
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,6 @@ Output :
2626
5
2727
The counter currently is at 14
2828
*/
29+
30+
31+

Large-Collection-of-Popular-Problems-with-Solutions/General-JS-Problems/raise-To-Power-bit-shifting.js

+9-15
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,16 @@
11
// Regular iterative approach
22

33
raiseToPower = (base, exponent) => {
4-
5-
let result = 1;
4+
let result = 1
65

76
for (let i = 1; i <= exponent; i++) {
8-
result *= base;
7+
result *= base
98
}
109

11-
return result;
10+
return result
1211
}
1312

14-
console.log(raiseToPower(5, 3));
15-
13+
console.log(raiseToPower(5, 3))
1614

1715
/* SOLUTION - 2 as given in - https://js-algorithms.tutorialhorizon.com/2015/10/23/implement-a-power-function/ - "If our exponent is 10,000 then our loop has to run 10,000 times. But we can optimize this logic to use only log2(10,000) ~ 14 times by using the binary form of the exponent."
1816
@@ -39,28 +37,25 @@ https://en.wikipedia.org/wiki/Bitwise_operation#AND
3937
4038
*/
4139

42-
4340
/* Steps - Optimized logic:
4441
If exponent’s last binary digit is one then multiply the result with base
4542
Divide the exponent by 2 by doing one right shift. Note from above, a single ritht shift means juse diving the number by 2
4643
Multiply base with itself
4744
*/
4845

4946
raiseToPower_binary = (base, exponent) => {
50-
5147
if (exponent < 0) {
52-
throw new Error('Negative exponent are not accepted')
48+
throw new Error("Negative exponent are not accepted")
5349
}
5450

55-
let result = 1;
51+
let result = 1
5652

5753
while (exponent) {
58-
5954
// Scan the binary representation of exponent on each iteration of the loop. If it finds a 1 then it multiplies the result by base (just like the iterative approach).
6055
// Finding a 1 means that the current value of exponent is not divisible by 2
6156

6257
if ((exponent & 1) === 1) {
63-
result *= base;
58+
result *= base
6459
}
6560

6661
// Divide the exponent by 2 by doing one right shift. Note from above, a single ritht shift means juse diving the number by 2
@@ -70,8 +65,7 @@ raiseToPower_binary = (base, exponent) => {
7065
base *= base
7166
}
7267

73-
return result;
74-
68+
return result
7569
}
7670

77-
console.log(raiseToPower_binary(5, 3));
71+
console.log(raiseToPower_binary(5, 3))

0 commit comments

Comments
 (0)