Skip to content

Commit 8f47780

Browse files
new samples added
1 parent 26bcd81 commit 8f47780

File tree

8 files changed

+117
-4
lines changed

8 files changed

+117
-4
lines changed

samples/common/leftpad.js

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
function leftpad (str, len, ch) {
2+
  str = String(str);
3+
4+
  var i = -1;
5+
  if (!ch && ch !== 0) ch = ' ';
6+
  len = len - str.length;
7+
  while (++i < len) {
8+
    str = ch + str;
9+
  }
10+
  return str;
11+
}
12+

samples/common/v8bigint.js

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
//bigint support
2+
const numberWithCommas = (x) => {
3+
return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ".");
4+
}
5+
6+
function main() {
7+
console.log("main");
8+
console.log((2n**31n/86400n)/365n);
9+
var foo= 2n**40n / (10n*100n*1000n);
10+
11+
var x = BigInt(Number.MAX_SAFE_INTEGER);
12+
13+
console.log(numberWithCommas(x));
14+
15+
console.error(numberWithCommas((2n**63n)/(1000n*86400n*365n)));
16+
17+
console.error(numberWithCommas((2n**31n)/(86400n*365n)));
18+
19+
20+
return numberWithCommas(foo);
21+
};

samples/common/v8class.js

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
function main() {
2+
const animal = new Animal('Cat')
3+
animal.printName(); // Cat
4+
Animal.beProud();
5+
console.info(JSON.stringify(animal));
6+
return animal;
7+
};
8+
9+
class Animal {
10+
 constructor(name) {
11+
   this.name = name
12+
 }
13+
//static legCount = 4
14+
15+
 static beProud() {
16+
   console.log('I AM AN ANIMAL')
17+
 }
18+
19+
 printName() {
20+
   console.log(this.name)
21+
 }
22+
}

samples/common/v8factor.js

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
function calculate(num) {
2+
   var str = "0";
3+
   for (i = 1; i <= num; i++) {
4+
       if (num % i == 0) {
5+
           str += ',' + i;
6+
       }
7+
   }
8+
   return str;
9+
}
10+
11+
function main(){
12+
return calculate( 2019 );
13+
}

samples/common/v8factorial.js

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
var factorial = function(number) {
2+
if (number <= 0) { // terminal case
3+
return 1;
4+
} else { // block to execute
5+
return (number * factorial(number - 1));
6+
}
7+
};
8+
9+
function main() {
10+
var r = factorial(8);
11+
return r;
12+
}
13+

samples/common/v8file.js

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
function main() {
2+
3+
_write("/storage/emulated/0/jstest/test.json", "hello world: "+ new Date());
4+
5+
var content = _read("/storage/emulated/0/jstest/test.json");
6+
7+
console.log(content);
8+
9+
return "";
10+
};

samples/common/v8input.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
//simple test function
21
function main() {
3-
_log("main");
2+
console.log("main");
3+
44
var ret = _inputForm("Input:");
55

6-
_log(parseInt(ret) + 1);
6+
console.log(parseInt(ret) + 1);
77

88
var foo = "V8: " + ret + " finished!"
99
return foo;
1010
};
1111

12-
12+

samples/common/v8loop.js

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
//!require(leftpad.js)
2+
3+
let max = 30;
4+
let text = "*du Lauch* 💥";
5+
let count = 2;
6+
7+
function print(i) {
8+
return leftpad(text + "\n" , text.length+i%max," ");
9+
}
10+
11+
function main() {
12+
var foo = "";
13+
text = _inputForm("Input:",text)
14+
for (var j=1; j<count; j++){
15+
for (i=1;i<=max-1;i++)
16+
foo += print(i);
17+
for (i=max-1;i>0;i--)
18+
foo += print(i);
19+
}
20+
return foo;
21+
};
22+

0 commit comments

Comments
 (0)