Skip to content

Instantly share code, notes, and snippets.

@bramses bramses/prblm.js Secret
Last active Jun 28, 2018

Embed
What would you like to do?
Divide and Conquer
class Problem {
// a constructor is whats called after an object is made
// here we used defaults to set our variables
constructor (value = '', points = default_points, { solved = false, solution = '', problems = [] } = {}) {
this.value = value;
this.solved = solved;
this.points = points;
this.solution = solution;
this.problems = problems;
}
// append a problem to the list
add_problem (problem) {
this.problems.push(problem);
}
// solve your problem!
solve () {
this.solved = true;
}
// is our problem solved?
isSolved () {
return this.solved;
}
}
// list of our problems starting with the smallest first
const small_problem_1 = new Problem('ez mode', 25, {solution: 'Put on my left shoe'});
const small_problem_2 = new Problem('also ez', 25, {solution: 'Take a deep breath in'});
const medium_problem_1 = new Problem('This is still too hard', 50, {problems: [small_problem_1, small_problem_2]});
const medium_problem_2 = new Problem('I agree, I\'d rather not be solved', 50, {problems: [small_problem_1, small_problem_2]});
const big_problem = new Problem('WE\'VE GOT A BIG PROBLEM', 100, {problems: [medium_problem_1, medium_problem_2]});
// solve our problems from the bottom up!
small_problem_1.solve();
console.log(`Small problem 1 solved?: ${small_problem_1.isSolved()}`);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.