Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Final Release #7

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
98 changes: 82 additions & 16 deletions async/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,35 +17,101 @@ class RNG {

return result;
}

static gatchaRoll(times,cb) {
let arr = []
for(let i = 0; i < times; i++) {
arr.push(this.roll())
}
arr.sort()
let result = 0
arr.forEach(num => {
if(num === 0) {
result = num
} else {
result = num
}
})
cb(result)
}

static gatchaRollPromise(times) {
return new Promise ((res,rej) => {
if(times > 0) {
let arr = []
for(let i = 0; i < times; i++) {
arr.push(this.roll())
}
arr.sort()
arr.forEach(num => {
let result = 0
if(num !== 0) {
result = num
res(result)
}
})
} else {
rej(times)
}
})
}
}

function viewGachaResult(best) {
console.log(`YOUR BEST GATCHA ROLL RESULT IS ${best}`);
setTimeout(function() {
// jalankan sesuatu disini setelah 1 detik
console.log(`YOUR BEST GATCHA ROLL RESULT IS ${best}`)
}, 1000);
}

function viewGachaFailure() {
console.log('YAKIN NGGAK MAU NGE-ROLL?');
setTimeout(function() {
// jalankan sesuatu disini setelah 1 detik
console.log('YAKIN NGGAK MAU NGE-ROLL?')
}, 1000);
}

// 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(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
// //
// // RELEASE 1 TEST CASES
// RNG.gatchaRollPromise(5)
// .then(function(result) { viewGachaResult(result) })
// .catch(function(err) { viewGachaFailure() });
//
// // akan menampilkan di log: YOUR BEST GATCHA ROLL RESULT IS <angka antara 1-5>
// //
// RNG.gatchaRollPromise(0)
// .then(function(result) { viewGachaResult(result) })
// .catch(function(err) { viewGachaFailure() });

// RELEASE 1 TEST CASES
RNG.gatchaRollPromise(5)
.then(function(result) { viewGachaResult(result) })
.catch(function(err) { viewGachaFailure() };
// akan menampilkan di log: YAKIN NGGAK MAU NGE-ROLL?

// akan menampilkan di log: YOUR BEST GATCHA ROLL RESULT IS <angka antara 1-5>

RNG.gatchaRollPromise(0)
.then(function(result) { viewGachaResult(result) })
.catch(function(err) { viewGachaFailure() };
// RELEASE 2 PROMISE(S)

// akan menampilkan di log: YAKIN NGGAK MAU NGE-ROLL?
// code here...

promise1 = RNG.gatchaRollPromise(5)
promise2 = RNG.gatchaRollPromise(1)
promise3 = RNG.gatchaRollPromise(2)
promise4 = RNG.gatchaRollPromise(3)
promise5 = RNG.gatchaRollPromise(4)
promise6 = RNG.gatchaRollPromise(2)
promise7 = RNG.gatchaRollPromise(2)
promise8 = RNG.gatchaRollPromise(3)
promise9 = RNG.gatchaRollPromise(2)
promise10 = RNG.gatchaRollPromise(3)

// RELEASE 2 PROMISE(S)
let arrOfPromise = [promise1,promise2,promise3,promise4,promise5,promise6,promise7,promise8,promise9,promise10]

// code here...
Promise.all(arrOfPromise)
.then(promises => {
viewGachaResult(promises)
console.log(promises);
})
.catch(err => {
console.log(err);
})
98 changes: 98 additions & 0 deletions oop/robot.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
'use strict'

class Robot {
constructor(name){
this.name = name
}
}

class Wall_E extends Robot {
constructor(name) {
super(name)
this.name = name || 'wall-E'
this.purpose = 'Worker'
}

work() {
console.log(`${this.name} cleans planet`)
}
}

class Baymax extends Robot {
constructor(name) {
super(name)
this.name = name || 'Baymax'
this.purpose = 'Medic'
}

heal() {
console.log(`Hi, I am ${this.name}, how may I help you?`)
}
}

class AutoBot extends Robot {
constructor(name) {
super(name)
this.name = name || 'AutoBot'
this.purpose = 'Defender'
}

defend() {
console.log(`${this.name} let's roll!!`)
}
}
//
// let wall_E = new Wall_E()
// let bayMax = new Baymax()
// let autoBot = new AutoBot()

class RobotFactory {
constructor(name,total) {
this.name = name
this.total = total
}

static produceRobot(name,total) {
if(name === 'wall-e') {
let arrOfRobot = []
for(let i = 0; i < total; i++) {
let robot = new Wall_E(name)
arrOfRobot.push(robot)
}
return arrOfRobot
} else if(name === 'baymax') {
let arrOfRobot = []
for(let i = 0; i < total; i++) {
let robot = new Baymax(name)
arrOfRobot.push(robot)
}
return arrOfRobot
} else if(name === 'autobot') {
let arrOfRobot = []
for(let i = 0; i < total; i++) {
let robot = new AutoBot(name)
arrOfRobot.push(robot)
}
return arrOfRobot
}
}
}
let wall_e = RobotFactory.produceRobot('wall-e', 6);
let baymax = RobotFactory.produceRobot('baymax', 5);
let autobot = RobotFactory.produceRobot('autobot', 3);
//
// console.log(wall_e);
// console.log(baymax);
// console.log(autobot);

for (var i = 0; i < wall_e.length; i++) {
wall_e[i].work()
}

for (var i = 0; i < baymax.length; i++) {
baymax[i].heal();
}

for (var i = 0; i < autobot.length; i++) {
autobot[i].defend();
}
Binary file modified query/database.db
Binary file not shown.
Loading