-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcodewarsChallenge60.js
More file actions
109 lines (91 loc) · 3.57 KB
/
codewarsChallenge60.js
File metadata and controls
109 lines (91 loc) · 3.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
class Tree {
constructor(size = "10", leaves = {spring: 'green', summer: "green", fall: "orange", winter: null}) {
this.size = size;
this.leaves = leaves;
this.leafColor = null;
}
changeSeason(season) {
this.leafColor = this.leaves[season];
if (season === "spring") {
this.size += 1;
}
return [this.syrupQty, this.size]; // Return size & syrupQty for logging
}
}
class Maple extends Tree {
constructor(syrupQty = 15, size, leaves) {
super(size, leaves); // Calls the parent Tree constructor
this.syrupQty = syrupQty;
}
changeSeason(season) {
super.changeSeason(season); // Call the parent method to change season
if (season === "spring") {
this.syrupQty += 1;
}
return [this.syrupQty, this.size]; // Return syrupQty & size for logging
}
gatherSyrup() {
this.syrupQty -= 3;
return this.syrupQty; // Return updated syrupQty for logging
}
}
const myMaple = new Maple(15, 5); // Creates a new Maple instance
// Call changeSeason on the myMaple instance
console.log(myMaple.changeSeason('fall')); // Outputs: [15, 5]
console.log(myMaple.changeSeason()); // This call does not provide argument, which may cause undefined behavior. If you want to see fall again, provide 'fall' or some valid season.
console.log(myMaple.changeSeason('spring')); // Outputs: [16, 6]
console.log(myMaple.gatherSyrup()); // Outputs: 13
// Using a Function to to the same
/*
function Tree(size, leaves) {
// Set the size property, default to 10 if size is undefined
this.size = (typeof size === "undefined") ? 10 : size;
// Define default leaves for the tree
const defaultLeaves = {spring: "green", summer: "green", fall: "orange", winter: null};
// Set the leaves property, default to defaultLeaves if leaves is undefined
this.leaves = (typeof leaves === "undefined") ? defaultLeaves : leaves;
// Initialize leafColor property
this.leafColor;
}
// Add changeSeason method to Tree prototype
Tree.prototype.changeSeason = function(season) {
// Set the leafColor based on the season
this.leafColor = this.leaves[season];
// If the season is spring, increase the size by 1
if (season === "spring") {
this.size += 1;
}
}
function Maple(syrupQty, size, leaves) {
// Call the Tree constructor with size and leaves
Tree.call(this, size, leaves);
// Set the syrupQty property, default to 15 if syrupQty is undefined
this.syrupQty = (typeof syrupQty === "undefined") ? 15 : syrupQty;
}
// Set the prototype of Maple to be an instance of Tree
Maple.prototype = Object.create(Tree.prototype);
// Set the constructor property of Maple prototype to Maple
Maple.prototype.constructor = Maple;
// Add changeSeason method to Maple prototype
Maple.prototype.changeSeason = function(season) {
// Call the changeSeason method of Tree
Tree.prototype.changeSeason.call(this, season);
// If the season is spring, increase the syrupQty by 1
if (season === "spring") {
this.syrupQty += 1;
}
}
// Add gatherSyrup method to Maple prototype
Maple.prototype.gatherSyrup = function() {
// Decrease the syrupQty by 3
this.syrupQty -= 3;
}
// Create a new instance of Maple with syrupQty 15 and size 5
const myMaple = new Maple(15, 5);
// Change the season to fall
myMaple.changeSeason('fall');
// Gather syrup, decreasing syrupQty by 3
myMaple.gatherSyrup();
// Change the season to spring
myMaple.changeSeason('spring');
*/