Repository navigation
Expand file tree
/
Copy pathtest-idea.js
More file actions
158 lines (112 loc) · 3.17 KB
/
Copy pathtest-idea.js
File metadata and controls
158 lines (112 loc) · 3.17 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
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
// function Coin(){
// this.type = "finance"
// this.keywords = [1,4,7]
// }
// function BitCoin(){
// this.name = "bitcoin"
// }
// BitCoin.prototype = new Coin();
// let b1 = new BitCoin();
// let b2 = new BitCoin();
// console.log(b1.keywords)
// console.log(b1.type)
// console.log(b2.keywords)
// console.log(b2.type)
// b1.keywords.push(10);
// console.log(b2.keywords)
// function MetalCoin(){
// this.type = "centralized finance"
// this.name = "metal coin"
// }
/*************************************** */
// function Coin(){
// this.type = "finance"
// this.keywords = [1,4,7]
// }
// Coin.prototype.trade = () => {console.log("coins for trade.")}
// function BitCoin(){
// Coin.call(this);
// this.name = "bitcoin"
// }
// BitCoin.prototype = new Coin();
// let b1 = new BitCoin();
// let b2 = new BitCoin();
// console.log(b1.keywords)
// console.log(b1.type)
// console.log(b2.keywords)
// console.log(b2.type)
// console.log(b1.trade())
// b1.keywords.push(10);
// console.log(b2.keywords)
/************************************************** */
// function Coin(){
// this.type = "finance"
// this.keywords = [1,4,7]
// }
// Coin.prototype.trade = () => {console.log("coins for trade.")}
// let coin = new Coin();
// let bitcoin = Object.create(coin);
// console.log(bitcoin.type)
// console.log(bitcoin.keywords)
// console.log(bitcoin.trade())
/**************************************************** */
// let parent = {
// name:"Dave",
// getName: function(){
// console.log(this.name)
// }
// }
// let child = Object.create(parent);
// console.log(child.getName());
/**************************************************** */
// Object.create() established prototype connection between the object returned and the parameter object
function Super(name){
this.name = name;
this.colors = ["red","blue"];
}
Super.prototype.sayHi = function(){
console.log(this.name);
}
// inherit properties, and pass parameters, so Sub can have its own names input
function Sub(name, age){
Super.call(this, name)
this.age = age;
}
inherit(Sub, Super);
inherit(Human, Sub)
function Human(name, age){
Sub.call(this, name, age)
this.honk = 8;
}
var person5 = new Sub("TOM", 26);
// object.create creates a copy of Super.prototype(an object), which only has methods, no properties
// all the properties like colors needs to run the constructor
// this way of writing inheritance only makes one Super constructor call
function inherit(child, parent){
child.prototype = Object.create(parent.prototype);
// child.prototype.constructor = child;
}
var human = new Human("Jerry", 9);
console.log(human.honk)
console.log(human.age)
console.log(human.name)
console.log(Sub.prototype.colors)
console.log(person5.colors)
console.log(person5.age)
person5.sayHi()
var subArrayRanges = function(nums) {
let res = 0
for (let i = 1; i < nums.length; i++) {
for (let j = 0; j < i; j++) {
let smallest = nums[i], biggest = nums[i]
for (let k = j; k < i; k++) {
console.log("k vcal",k);
smallest = Math.min(smallest, nums[k])
biggest = Math.max(biggest, nums[k])
}
res += biggest - smallest
}
}
return res
};
console.log(subArrayRanges([1,5,3]));