diff --git a/async/index.js b/async/index.js index 77df3b7..967f85c 100644 --- a/async/index.js +++ b/async/index.js @@ -2,7 +2,7 @@ class RNG { static roll() { const tiers = [1, 2, 3, 4, 5]; let num = Math.floor(Math.random() * 100) + 1; - let result = 'UNKNOWN'; + let result = 0; if(num > 60) { result = tiers[0]; } else if(num > 40) { @@ -14,11 +14,50 @@ class RNG { } else { result = tiers[4]; } + return result +} - return result; + static gatchaRoll(times,callback){ + let best = 0 + var counter = setInterval(function(){ + if(times === 0){ + callback(best) + clearInterval(counter) + } else { + let a = RNG.roll() + // console.log(`dapet kocokan ${a}`); + if(a > best) { + best = a + } + times -= 1 + } + },1000) + return '' } + + static gatchaRollPromise(times){ + return new Promise((resolve, reject) => { + let best = 0 + let count = setInterval(() => { + if (times < 0) { + resolve(viewGachaResult(best)); + clearInterval(count); + } else { + let a = RNG.roll() + // console.log(`dapet kocokan ${a}`); + if(a > best) { + best = a + } + times -= 1 + } + }, 1000); + }) + } } + + + function viewGachaResult(best) { console.log(`YOUR BEST GATCHA ROLL RESULT IS ${best}`); } @@ -29,23 +68,34 @@ function viewGachaFailure() { // RELEASE 0 TEST CASES RNG.gatchaRoll(5, function(result) { viewGachaResult(result) }); // output log sesuai hasil random terbaik -RNG.gatchaRoll(1, function(result) { viewGachaResult(result) }); // output log sesuai hasil random terbaik -RNG.gatchaRoll(0, function(result) { viewGachaResult(result) }); // output: 0 +// RNG.gatchaRoll(1, function(result) { viewGachaResult(result) }); // output log sesuai hasil random terbaik +// RNG.gatchaRoll(0, function(result) { viewGachaResult(result) }); // output: 0 // RELEASE 1 TEST CASES -RNG.gatchaRollPromise(5) - .then(function(result) { viewGachaResult(result) }) - .catch(function(err) { viewGachaFailure() }; +// RNG.gatchaRollPromise(5) +// .then(function(result) { viewGachaResult(result) }) +// .catch(function(err) { viewGachaFailure() }); // akan menampilkan di log: YOUR BEST GATCHA ROLL RESULT IS RNG.gatchaRollPromise(0) .then(function(result) { viewGachaResult(result) }) - .catch(function(err) { viewGachaFailure() }; + .catch(function(err) { viewGachaFailure() }); // akan menampilkan di log: YAKIN NGGAK MAU NGE-ROLL? // RELEASE 2 PROMISE(S) +let arr = [] +for(let i = 0;i<10;i++){ + arr.push(RNG.gatchaRollPromise(5)) +} -// code here... \ No newline at end of file +Promise.all(arr) +.then(function(data){ + data +}) +.catch(function(err){ + console.log(err); +}) +// code here... diff --git a/oop/autobot.js b/oop/autobot.js new file mode 100644 index 0000000..24c97a9 --- /dev/null +++ b/oop/autobot.js @@ -0,0 +1,16 @@ +const Robot = require('./robot.js') + +class Autobot extends Robot { + constructor(name) { + super(name) + this.purpose = 'defend' + } + defend(){ + console.log("AutoBot, let's roll!") + } +} + +// var a = new Autobot('Bee','defender') +// a.defend() + +module.exports = Autobot; diff --git a/oop/baymax.js b/oop/baymax.js new file mode 100644 index 0000000..415d42a --- /dev/null +++ b/oop/baymax.js @@ -0,0 +1,15 @@ +const Robot = require('./robot.js') + +class Baymax extends Robot { + constructor(name) { + super(name) + this.purpose = 'medic' + } + heal(){ + console.log('Hi, I am BayMax, how may I help you?') + } +} + +// let a = new Baymax('baymax') +// a.heal() +module.exports = Baymax; diff --git a/oop/index.js b/oop/index.js new file mode 100644 index 0000000..e69de29 diff --git a/oop/robot.js b/oop/robot.js new file mode 100644 index 0000000..dc45dba --- /dev/null +++ b/oop/robot.js @@ -0,0 +1,7 @@ +class Robot { + constructor(name) { + this.name = name + } +} + +module.exports = Robot; diff --git a/oop/robot_factory.js b/oop/robot_factory.js new file mode 100644 index 0000000..06f4c57 --- /dev/null +++ b/oop/robot_factory.js @@ -0,0 +1,45 @@ +const Wall = require('./wall-e.js') +const Baymax = require('./baymax.js') +const Autobot = require('./autobot.js') + +class RobotFactory { + constructor() { + } + + static produceRobot(name,amount){ + let arr = [] + if(name === 'wall-e'){ + for(let i =0;i{ + if(!err){ + console.log(data) + }else{ + console.log(err) + } + }) +} + + + +// sortPlayer() +function cardCount(){ + db.all(` SELECT COUNT(Cards.PlayerId) As totalcard, + Cards.playerId, Players.name,Players.age,Players.gender + FROM Cards + LEFT JOIN Players ON Players.id = Cards.playerId + WHERE Players.gender = 'Male' + GROUP BY Cards.playerId + ORDER by totalcard DESC + LIMIT 3 + `,(err,data)=>{ + if(!err){ + console.log(data) + }else{ + console.log(err) + } + }) +} +cardCount()