diff --git a/challenges/02-javascript-algorithms-and-data-structures/object-oriented-programming.json b/challenges/02-javascript-algorithms-and-data-structures/object-oriented-programming.json index 1b48605dce7..d6b125bcc05 100644 --- a/challenges/02-javascript-algorithms-and-data-structures/object-oriented-programming.json +++ b/challenges/02-javascript-algorithms-and-data-structures/object-oriented-programming.json @@ -57,7 +57,9 @@ "assert(typeof(dog.name) === 'string', 'message: dog should have a name property set to a string.');", "assert(typeof(dog.numLegs) === 'number', 'message: dog should have a numLegs property set to a number.');" ], - "solutions": [], + "solutions": [ + "let dog = {\n name: '',\n numLegs: 4\n};" + ], "hints": [], "type": "waypoint", "challengeType": 1, @@ -86,7 +88,9 @@ "assert(code.match(/console\\.log\\(dog\\.name\\)/g), 'message: Your code should use a console.log statement to print the value for the name property of the dog object.');", "assert(code.match(/console\\.log\\(dog\\.numLegs\\)/g), 'message: Your code should use a console.log statement to print the value for the numLegs property of the dog object.');" ], - "solutions": [], + "solutions": [ + "let dog = {\n name: \"Spot\",\n numLegs: 4\n};\nconsole.log(dog.name);\nconsole.log(dog.numLegs);" + ], "hints": [], "type": "waypoint", "challengeType": 1, @@ -117,7 +121,9 @@ "assert(typeof(dog.sayLegs) === 'function', 'message: dog.sayLegs() should be a function.');", "assert(dog.sayLegs() === 'This dog has 4 legs.', 'message: dog.sayLegs() should return the given string - note that punctuation and spacing matter.');" ], - "solutions": [], + "solutions": [ + "let dog = {\n name: \"Spot\",\n numLegs: 4,\n sayLegs () {\n return 'This dog has ' + this.numLegs + ' legs.';\n }\n};\n\ndog.sayLegs();" + ], "hints": [], "type": "waypoint", "challengeType": 1, @@ -151,7 +157,9 @@ "assert(dog.sayLegs() === 'This dog has 4 legs.', 'message: dog.sayLegs() should return the given string.');", "assert(code.match(/this\\.numLegs/g), 'message: Your code should use the this keyword to access the numLegs property of dog.');" ], - "solutions": [], + "solutions": [ + "let dog = {\n name: \"Spot\",\n numLegs: 4,\n sayLegs () {\n return 'This dog has ' + this.numLegs + ' legs.';\n }\n};\n\ndog.sayLegs();" + ], "hints": [], "type": "waypoint", "challengeType": 1, @@ -184,7 +192,9 @@ "assert(typeof (new Dog()).color === 'string', 'message: Dog should have a color property set to a string.');", "assert(typeof (new Dog()).numLegs === 'number', 'message: Dog should have a numLegs property set to a number.');" ], - "solutions": [], + "solutions": [ + "function Dog (name, color, numLegs) {\n this.name = 'name';\n this.color = 'color';\n this.numLegs = 4;\n}" + ], "hints": [], "type": "waypoint", "challengeType": 1, @@ -218,7 +228,9 @@ "assert(hound instanceof Dog, 'message: hound should be created using the Dog constructor.');", "assert(code.match(/new/g), 'message: Your code should use the new operator to create an instance of Dog.');" ], - "solutions": [], + "solutions": [ + "function Dog() {\n this.name = \"Rupert\";\n this.color = \"brown\";\n this.numLegs = 4;\n}\nconst hound = new Dog();" + ], "hints": [], "type": "waypoint", "challengeType": 1, @@ -255,7 +267,9 @@ "assert((new Dog('Clifford')).numLegs === 4, 'message: Dog should have property numLegs set to 4.');", "assert(terrier instanceof Dog, 'message: terrier should be created using the Dog constructor.');" ], - "solutions": [], + "solutions": [ + "function Dog (name, color) {\n this.numLegs = 4;\n this.name = name;\n this.color = color;\n}\n\nconst terrier = new Dog();" + ], "hints": [], "type": "waypoint", "challengeType": 1, @@ -283,9 +297,11 @@ ], "tests": [ "assert(typeof myHouse.numBedrooms === 'number', 'message: myHouse should have a numBedrooms attribute set to a number.');", - "assert(editor.getValue().match(/(myHouse instanceof House)/), 'message: Be sure to verify that myHouse is an instance of House using the instanceof operator.');" + "assert(/myHouse\\s*instanceof\\s*House/.test(code), 'message: Be sure to verify that myHouse is an instance of House using the instanceof operator.');" + ], + "solutions": [ + "function House(numBedrooms) {\n this.numBedrooms = numBedrooms;\n}\nconst myHouse = new House(4);\nconsole.log(myHouse instanceof House);" ], - "solutions": [], "hints": [], "type": "waypoint", "challengeType": 1, @@ -320,7 +336,9 @@ "tests": [ "assert(ownProps.includes('name') && ownProps.includes('numLegs'), 'message: ownProps should include the values \"numLegs\" and \"name\".');" ], - "solutions": [], + "solutions": [ + "function Bird(name) {\n this.name = name;\n this.numLegs = 2;\n}\n\nlet canary = new Bird(\"Tweety\");\nfunction getOwnProps (obj) {\n const props = [];\n \n for (let prop in obj) {\n if (obj.hasOwnProperty(prop)) {\n props.push(prop);\n }\n }\n \n return props;\n}\n\nconst ownProps = getOwnProps(canary);" + ], "hints": [], "type": "waypoint", "challengeType": 1, @@ -357,7 +375,9 @@ "assert(typeof(beagle.numLegs) === 'number' , 'message: beagle.numLegs should be a number.');", "assert(beagle.hasOwnProperty('numLegs') === false, 'message: numLegs should be a prototype property not an own property.');" ], - "solutions": [], + "solutions": [ + "function Dog (name) {\n this.name = name;\n}\nDog.prototype.numLegs = 4;\nlet beagle = new Dog(\"Snoopy\");" + ], "hints": [], "type": "waypoint", "challengeType": 1, @@ -395,7 +415,9 @@ "assert(ownProps.includes('name'), 'message: The ownProps array should include \"name\".');", "assert(prototypeProps.includes('numLegs'), 'message: The prototypeProps array should include \"numLegs\".');" ], - "solutions": [], + "solutions": [ + "function Dog(name) {\n this.name = name;\n}\n\nDog.prototype.numLegs = 4;\n\nlet beagle = new Dog(\"Snoopy\");\n\nlet ownProps = [];\nlet prototypeProps = [];\nfor (let prop in beagle) {\n if (beagle.hasOwnProperty(prop)) {\n ownProps.push(prop);\n } else {\n prototypeProps.push(prop);\n }\n}" + ], "hints": [], "type": "waypoint", "challengeType": 1, @@ -429,7 +451,9 @@ "assert(typeof(joinDogFraternity) === 'function', 'message: joinDogFraternity should be defined as a function.');", "assert(joinDogFraternity(new Dog(\"\")) === true, 'message: joinDogFraternity should return true ifcandidate is an instance of Dog.');" ], - "solutions": [], + "solutions": [ + "function Dog(name) {\n this.name = name;\n}\nfunction joinDogFraternity(candidate) {\n return candidate.constructor === Dog;\n}" + ], "hints": [], "type": "waypoint", "challengeType": 1, @@ -464,7 +488,9 @@ "assert(Dog.prototype.eat !== undefined, 'message: Dog.prototype should have the property eat.'); ", "assert(Dog.prototype.describe !== undefined, 'message: Dog.prototype should have the property describe.'); " ], - "solutions": [], + "solutions": [ + "function Dog(name) {\n this.name = name; \n}\nDog.prototype = {\nnumLegs: 4,\n eat () {\n console.log('nom nom nom');\n },\n describe () {\n console.log('My name is ' + this.name);\n }\n};" + ], "hints": [], "type": "waypoint", "challengeType": 1, @@ -501,7 +527,9 @@ "tests": [ "assert(Dog.prototype.constructor === Dog, 'message: Dog.prototype should set the constructor property.');" ], - "solutions": [], + "solutions": [ + "function Dog(name) {\n this.name = name; \n}\nDog.prototype = {\n constructor: Dog,\n numLegs: 2, \n eat: function() {\n console.log(\"nom nom nom\"); \n }, \n describe: function() {\n console.log(\"My name is \" + this.name); \n }\n};" + ], "hints": [], "type": "waypoint", "challengeType": 1, @@ -532,7 +560,9 @@ "tests": [ "assert(/Dog\\.prototype\\.isPrototypeOf\\(beagle\\)/.test(code), 'message: Show that Dog.prototype is the prototype of beagle');" ], - "solutions": [], + "solutions": [ + "function Dog(name) {\n this.name = name;\n}\nlet beagle = new Dog(\"Snoopy\");\nDog.prototype.isPrototypeOf(beagle);" + ], "hints": [], "type": "waypoint", "challengeType": 1, @@ -570,7 +600,9 @@ "tests": [ "assert(/Object\\.prototype\\.isPrototypeOf/.test(code), \"message: Your code should show that Object.prototype is the prototype of Dog.prototype\");" ], - "solutions": [], + "solutions": [ + "function Dog(name) {\n this.name = name;\n}\nlet beagle = new Dog(\"Snoopy\");\nDog.prototype.isPrototypeOf(beagle);\nObject.prototype.isPrototypeOf(Dog.prototype);" + ], "hints": [], "type": "waypoint", "challengeType": 1, @@ -625,7 +657,9 @@ "assert(!(Bear.prototype.hasOwnProperty('eat')), 'message: Bear.prototype should not have the eat property.');", "assert(!(Cat.prototype.hasOwnProperty('eat')), 'message: Cat.prototype should not have the eat property.');" ], - "solutions": [], + "solutions": [ + "function Cat(name) {\n this.name = name; \n}\n\nCat.prototype = {\n constructor: Cat\n};\n\nfunction Bear(name) {\n this.name = name; \n}\n\nBear.prototype = {\n constructor: Bear\n};\n\nfunction Animal() { }\n\nAnimal.prototype = {\n constructor: Animal,\n eat: function() {\n console.log(\"nom nom nom\");\n }\n};" + ], "hints": [], "type": "waypoint", "challengeType": 1, @@ -672,7 +706,9 @@ "assert(duck instanceof Animal, 'message: duck should have a prototype of Animal.');", "assert(beagle instanceof Animal, 'message: beagle should have a prototype of Animal.');" ], - "solutions": [], + "solutions": [ + "function Animal() { }\n\nAnimal.prototype = {\n constructor: Animal, \n eat: function() {\n console.log(\"nom nom nom\");\n }\n};\nlet duck = Object.create(Animal.prototype);\nlet beagle = Object.create(Animal.prototype);\n\nduck.eat();\nbeagle.eat();" + ], "hints": [], "type": "waypoint", "challengeType": 1, @@ -712,7 +748,9 @@ "tests": [ "assert(Animal.prototype.isPrototypeOf(Dog.prototype), 'message: Dog.prototype should be an instance of Animal.');" ], - "solutions": [], + "solutions": [ + "function Animal() { }\n\nAnimal.prototype = {\n constructor: Animal,\n eat: function() {\n console.log(\"nom nom nom\");\n }\n};\n\nfunction Dog() { }\nDog.prototype = Object.create(Animal.prototype);\n\nlet beagle = new Dog();\nbeagle.eat();" + ], "hints": [], "type": "waypoint", "challengeType": 1, @@ -751,7 +789,9 @@ "assert(Animal.prototype.isPrototypeOf(Dog.prototype), 'message: Dog.prototype should be an instance of Animal.');", "assert(beagle.constructor === Dog, 'message: beagle.constructor should return Dog.');" ], - "solutions": [], + "solutions": [ + "function Animal() { }\nfunction Bird() { }\nfunction Dog() { }\nBird.prototype = Object.create(Animal.prototype);\nDog.prototype = Object.create(Animal.prototype);\nDog.prototype.constructor = Dog;\nBird.prototype.constructor = Bird;\nlet duck = new Bird();\nlet beagle = new Dog();" + ], "hints": [], "type": "waypoint", "challengeType": 1, @@ -796,7 +836,9 @@ "assert(beagle instanceof Animal, 'message: beagle should be an instanceof Animal.');", "assert(beagle.constructor === Dog, 'message: The constructor for beagle should be set to Dog.');" ], - "solutions": [], + "solutions": [ + "function Animal() { }\nAnimal.prototype.eat = function() { console.log(\"nom nom nom\"); };\n\nfunction Dog() { }\nDog.prototype = Object.create(Animal.prototype);\nDog.prototype.constructor = Dog;\nDog.prototype.bark = function () {\n console.log('Woof!');\n};\nlet beagle = new Dog();\n\nbeagle.eat();\nbeagle.bark();" + ], "hints": [ "Objects inherit methods from other objects by cloning their prototype. The Object.create method will come in handy, and don't forget to reset the constructor property afterward!" ], @@ -845,7 +887,9 @@ "assert(penguin.fly() === \"Alas, this is a flightless bird.\", 'message: penguin.fly() should return the string \"Alas, this is a flightless bird.\"');", "assert((new Bird()).fly() === \"I am flying!\", 'message: The bird.fly() method should return \"I am flying!\"');" ], - "solutions": [], + "solutions": [ + "function Bird() { }\n\nBird.prototype.fly = function() { return \"I am flying!\"; };\n\nfunction Penguin() { }\nPenguin.prototype = Object.create(Bird.prototype);\nPenguin.prototype.constructor = Penguin;\nPenguin.prototype.fly = () => 'Alas, this is a flightless bird.';\nlet penguin = new Penguin();\nconsole.log(penguin.fly());" + ], "hints": [], "type": "waypoint", "challengeType": 1, @@ -890,7 +934,9 @@ "assert(typeof bird.glide === \"function\", 'message: Your code should use the glideMixin on the bird object to give it the glide method.');", "assert(typeof boat.glide === \"function\", 'message: Your code should use the glideMixin on the boat object to give it the glide method.');" ], - "solutions": [], + "solutions": [ + "let bird = {\n name: \"Donald\",\n numLegs: 2\n};\n\nlet boat = {\n name: \"Warrior\",\n type: \"race-boat\"\n};\nfunction glideMixin (obj) {\n obj.glide = () => 'Gliding!';\n}\n\nglideMixin(bird);\nglideMixin(boat);" + ], "hints": [], "type": "waypoint", "challengeType": 1, @@ -921,7 +967,9 @@ "assert(!code.match(/this\\.weight/g), 'message: The weight property should be a private variable.');", "assert((new Bird()).getWeight() === 15, 'message: Your code should create a method in Bird called getWeight that returns the weight.');" ], - "solutions": [], + "solutions": [ + "function Bird() {\n let weight = 15;\n \n this.getWeight = () => weight;\n}" + ], "hints": [], "type": "waypoint", "challengeType": 1, @@ -948,7 +996,9 @@ "assert(/\\(\\s*?function\\s*?\\(\\s*?\\)\\s*?{/.test(code), 'message: The function should be anonymous.');", "assert(/}\\s*?\\)\\s*?\\(\\s*?\\)/.test(code), 'message: Your function should have parentheses at the end of the expression to call it immediately.');" ], - "solutions": [], + "solutions": [ + "(function () {\n console.log(\"A cozy nest is ready\");\n})();" + ], "hints": [], "type": "waypoint", "challengeType": 1, @@ -985,7 +1035,9 @@ "assert(typeof funModule.isCuteMixin === \"function\", 'message: funModule.isCuteMixin should access a function.');", "assert(typeof funModule.singMixin === \"function\", 'message: funModule.singMixin should access a function.');" ], - "solutions": [], + "solutions": [ + "const funModule = (function () {\n return {\n isCuteMixin: obj => {\n obj.isCute = () => true;\n },\n singMixin: obj => {\n obj.sing = () => console.log(\"Singing to an awesome tune\");\n }\n };\n})();" + ], "hints": [], "type": "waypoint", "challengeType": 1,