Homework
JavaScript Classes and Methods - Interactive Homework
In this notebook, you will learn about classes and methods in JavaScript. After the lesson, complete the tasks by editing the code cells.
What are Classes and Methods?
- Class: A blueprint for creating objects with properties and methods.
- Method: A function inside a class that defines an object’s behavior.
%%js
// Fridge Mini-Game: Enhanced with scores and quality feedback
//place class + constructor here
class Fridge {
constructor() {
// Array of items in the fridge with their scores
this.fridgeItems = [ //Write in ten items found in a fridge and assign them a value of 1-10 (values can repeat)
{name: "Piece of pizza", score: 10}, // Its more fun if it FUNNY
{name: "Snom", score: 10},//snom needs to stay refrigerated. Snom is also awesome. I would do 11 if I could
{name: "Leftover microwave ramen", score: 10},
{name: "Orange Juice", score: 5},
{name: "Carrots", score: 3},
{name: "Ranch Dressing", score: 9},
{name: "Strawberries", score: 4},
{name: "Grapes", score: 1},
{name: "Cheese", score: 5},
{name: "Salami", score: 7},
];
}
// Method to pick a random item from the fridge
randomItem() {
let randomIndex = Math.floor(Math.random() * this.fridgeItems.length);
return this.fridgeItems[randomIndex];
}
// Method to classify the find based on its score
classifyFind(score) { // Create an if -> return -> else -> return loop using youe scoring system to create an output of good, meh, bad
if (score > 6){
return ("Yes! Gooood!!!");
} else if (score > 3) {
return ("Eh, meh. Not bad, though.");
} else {
return ("...Yeah, uhm, I dunno about this... I'll eat it if I have to, I guess...");
}
}
// Reference method to start the game (Some fill in the blank)
fridgeGame() {
console.log("Hmm... what do I want to eat? Let's look in the fridge!"); // Write a response
const item = this.randomItem();
console.log(`Oh, looks like I got ${item.name}!`); //fill in the () with a phrase on the item and a reference to the item
// Get feedback based on the item's score (unchanged you only need to fix the {} )
// find and fix the misisng pieces
const feedback = this.classifyFind(item.score);
console.log(`My judgement: ${feedback} Score: ${item.score}`);
}
}
// Create an instance of the game hint: const.
const fridgeGame = new Fridge();
// Start the game by calling the play method hint .play
fridgeGame.fridgeGame() ;
<IPython.core.display.Javascript object>