JavaScript Classes and Methods - Interactive Popcorns
In this notebook, you will learn about classes and methods in JavaScript. During the lesson, use these to enhance your understanding of clases and methods ###
- Objectivs: Do something - Change Later
- How:
%%js
class fridge{
constructor(width, height){
this.width=width;
this.height=height;
}
}
let rect1 = new fridge(20, 40);
let rect2 = new fridge(10,20);
console.log(`${rect1.width} , ${rect1.height}`);
<IPython.core.display.Javascript object>
%%js
class rect{
constructor(area){
this.area = area;
}
getSides() {
let sides = [];
for(let i = 1; i <= Math.floor(Math.sqrt(this.area)); i++){
if(this.area % i === 0){
let w = i;
let h = this.area/i;
let dimensions = `(${w}, ${h})`;
sides.push(dimensions);
}
}
return sides;
}
}
let rect1 = new rect(28);
console.log(rect1.getSides());
<IPython.core.display.Javascript object>
%%js
class Rectangle {
constructor(width, height) {
this.width = width;
this.height = height;
}
// **Code missing**: Add method to calculate the perimeter of the rectangle
getPerimeter() {
return 2*this.width+2*this.height;
}
}
const rect = new Rectangle(10, 5);
console.log(rect.getPerimeter()); // Output should be 30
<IPython.core.display.Javascript object>
%%js
class Square {
constructor(sideLength) {
this.sideLength = sideLength;
}
// **Code missing**: Add method to calculate the diagonal of the square
getDiagonal() {
return Math.sqrt(2*this.sideLength);
}
}
const square = new Square(10);
console.log(square.getDiagonal());
<IPython.core.display.Javascript object>