File size: 7,108 Bytes
753c675
 
2636412
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bf44493
2636412
bf44493
2636412
bf44493
2636412
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4513766
2636412
 
4513766
2636412
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
753c675
2636412
 
 
f219435
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a5ee9be
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306

function main() {
    console.log("Hello World!");
}
main()
var main = function() {
    console.log("Hello World!2");
}
main()

/* ========================================================================== */
/*                                  if else:                                  */
/* ========================================================================== */

var a = 5 > 4 ? 'yes' : 'no';
var b = 10 == '100' ? 'yes': 'no';
console.log(b, 'aaah', a)


main(85)
main(44)
main(98)

function main(grade) {
    if (grade < 60) {
        console.log('F');
    } else if (grade < 70) {
        console.log('D');
    } else if (grade < 80) {
        console.log('C');
    } else if (grade < 90) {
        console.log('B');
    } else  console.log('A');
}

 /* ========================================================================== */
 /*                                 for loops;                                 */
 /* ========================================================================== */

for (let i = 0; i < 10; i++ ) {  // for (assign, while, function apply)
    console.log(i);
}

for (let i = 100; i >= 0; i-=5 ) {
    console.log(i);
}

var data = [3, 7, 2, 9, 1, 11];
var sum = 0;
data.forEach(function(d){
    sum += d;
    console.log(sum);
});

var t = "Hello World";
for (let c of t) {
    console.log(c);
}



var i=0
console.log('yes', i++<4, i, '\n') // i++ ==> i = i+1
while (i<5, i++<4) {
    i += 2
    console.log(i)
}


var i=0
var q="a string that cool"
do{
    i++
    console.log(q.charAt(2),  m = q.split(/\s/)) 
    for (let b = 0; (b<=1) | (b < m.length); b++) {
        console.log(m[b])
    }
}while(i<1)

let c ={id: 9, type: "f"}
// c to f temperature.
function main(){
    var fahr;
    // console.log(c.id);
    fahr = prompt("Enter the temperature in F: ");
    const ratio = 5.0/9.0;
    let cel = (fahr - 32) * ratio;
    writeln("The temperature in C is: " + cel);
}

main()



function removeVowels(s) {
    const vowels = "aeiouAEIOU";
    let sWithoutVowels = "";
    for (let eachChar of s) {
        // console.log(s, '===', eachChar, '===', vowels.indexOf(eachChar))
        if (vowels.indexOf(eachChar) === -1) {
            sWithoutVowels = sWithoutVowels + eachChar
        }
    }
    return sWithoutVowels
}

console.log(removeVowels("asjdiajisjd1ijinfss"))


// array & set.

var s = new String('0');
console.log(s??false);
console.log(s);


var arr = []
console.log(arr instanceof Array, Array.isArray(arr))

var set = new Set([1, 2]) 
console.log(typeof set, typeof Array.from(set))
console.log(Array.from(set))

/* ========================================================================== */
/*                                    数组去重:                                */
/* ========================================================================== */
var arr = [1, 2, 2, 4, 9, 6, 7, 5, 2, 3, 5, 6, 5]
// console.log(Array.from(new Set(arr))) 
console.log(arr, '\n',(new Set(arr))) 
console.log(Array.from(new Set(arr))) 
// set() 就可以去重

//2. write a function;

var arr = [1, 2, 2, 4, 9, 6, 7, 5, 2, 3, 5, 6, 5]
function gunique1(arr){
    let newArr = [-1]
    let arrsort= arr.sort()
    for (let i=0; i<arr.length; i++){
        if (arrsort[i] > newArr[newArr.length - 1]){
            newArr.push(arrsort[i])
            // console.log(arrsort[i], newArr[newArr.length - 1], arrsort[i] > newArr[newArr.length - 1])
        }
    }
    newArr.shift() // removes the first element of an array
    return console.log(newArr)
};

gunique1(arr)

//
var arr = [1, 2, 2, 4, 9, 6, 7, 5, 2, 3, 5, 6, 5]
function unique(arr) {
    arr.sort()
    let newArr = [arr[0]]
    for (let i = 0; i < arr.length; i++) {
        let item = arr[i] // item = 0 now.
        if (newArr[newArr.length - 1] !== item) {
            newArr.push(item)
        }
    }
    return newArr
}
unique(arr)

// flatten array
var arr = [1, 2, [3, [4, 5]]]
console.log(arr.flat(Infinity))


var arr = [1, 2, [3, [4, 5]]]
function flat(arr) {
    let str = JSON.stringify(arr).replace(/[\[|\]]/g, '')
    str = `[${str}]`
    return JSON.parse(str)
}
console.log(flat(arr))



/* ========================================================================== */
/*                            arrow functions in js                           */
/* ========================================================================== */
var materials = [
    'Hydrogen',
    'Helium',
    'Lithium',
    'Beryllium'
];

console.log(materials.map(material => material.length));
// expected output: Array [8, 6, 7, 9]


(function (a) {
    return console.log(a + 100);
})(3)

// (a) => {
//     return console.log(a + 100);
// }; //easier version. a => a + 100;


var materials = [
    'Hydrogen',
    'Helium',
    'Lithium',
    'Beryllium'
];
var b = ['sda','asda','asd', 'bsdf'] 
b.map((a,b)=>console.log(a+b+'aaaa')) // arrow function second element is index.

// sort arr

var arr = [2, 7, 0, 6, 1, 4, 8, 3]
console.log((arr) instanceof Array, typeof arr)
console.log(arr.sort())
console.log(arr.sort().reverse())
console.log("==========================================================================")
console.log(arr.sort((a, b) => a - b)) // 递增
console.log(arr.sort((a, b) => b - a)) // 递减
console.log(arr.sort((a, b) => b - a > 0)) // 递减 这个hide了一个条件.


/* ========================================================================== */
/*                                    class                                   */
/* ========================================================================== */

function Animal(name, size) {
    this.name = name
    this.size = size
}

Animal.prototype.eat = function (food) {
    console.log(this.name + "is eating " + food, "and " + this.name +' is getting fatter of ' + this.size)
}

var ani = new Animal('xiaoyou', '15 kilo') 
ani.eat('banana')
// xiaoyouis eating banana and xiaoyou is getting fatter of 15 kilo

class Rectangle {
    constructor(height, width) {
        this.height = height;
        this.width = width;
    }
    // Getter
    get area() {
        return this.calcArea();
    }
    // Method
    calcArea() {
      return this.height * this.width;
    }

    calcVolume(length){
        return this.height * this.width * length;
    }
}
const square = new Rectangle(10, 10);
console.log(square.area); // 100
console.log(square.calcVolume(22)); // 2200


console.log(Array(9).length)

var a = (Array(9).fill(null))
var k = (a.length===1 ? '91':'hahhah')
console.log(k)


// \\ operatopr()

let x = 3;
var z = ++x;
var y = x++;

console.log(`x:${x}, y:${y}, z:${z}`);
// expected output: "x:4, y:3"


const car = { make: 'Honda', model: 'Accord', year: 1998 };
console.log('make' in car);
delete car.make;
if ('make' in car === false && 'model' in car !== true) {
    car.make = 'Suzuki';
} else if ('model' in car ) {
    car.make = 'Benz';
};
console.log(car.make);
//\\




function haha(x){
    if (x){
        console.log('x is valid')
    }else{
        console.log('empty passed')
    };
    return;
}
q = haha()
if(q == null){console.log('returned undefined aka null')}