stlit / jsfiles /js_script.js
fyenne's picture
commit, with xo board game finished
a5ee9be
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')}