Repository navigation
Sprint-Challenge--JavaScript -- Borja soler #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
24475d1
2800173
1ad0103
66a2c05
fa7e51c
6f527a4
228f768
d136538
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,40 @@ | ||
| // 1. Copy and paste your prototype in here and refactor into class syntax. | ||
| class CuboidMaker { | ||
| constructor(attributes) { | ||
| this.length = attributes.length; | ||
| this.width = attributes.width; | ||
| this.height = attributes.height; | ||
| } | ||
| volume() { | ||
| return this.length * this.width * this.height; | ||
| } | ||
| surfaceArea() { | ||
| return ( | ||
| 2 * | ||
| (this.length * this.width + | ||
| this.length * this.height + | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. what you have done is fine but yo can/should also use brackets to mark the separation between 2 operator. so, it's easier to read. you can do something like
|
||
| this.width * this.height) | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| // Test your volume and surfaceArea methods by uncommenting the logs below: | ||
| // console.log(cuboid.volume()); // 100 | ||
| // console.log(cuboid.surfaceArea()); // 130 | ||
| console.log(cuboid.volume()); // 100 | ||
| console.log(cuboid.surfaceArea()); // 130 | ||
|
|
||
| // Stretch Task: Extend the base class CuboidMaker with a sub class called CubeMaker. Find out the formulas for volume and surface area for cubes and create those methods using the dimension properties from CuboidMaker. Test your work by logging out your volume and surface area. | ||
| // Stretch Task: Extend the base class CuboidMaker with a sub class called CubeMaker. Find out the formulas for volume and surface area for cubes and create those methods using the dimension properties from CuboidMaker. Test your work by logging out your volume and surface area. | ||
| class CubeMaker extends CuboidMaker { | ||
| constructor(attributes) { | ||
| super(attributes, attributes, attributes); | ||
| } | ||
| volume() { | ||
| return Math.pow(this.length || this.height || this.width, 3); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you can also use exponential operator
|
||
| } | ||
| surfaceArea() { | ||
| return Math.pow(this.length || this.height || this.width, 2) * 6; | ||
| } | ||
| } | ||
|
|
||
| const cube = new CubeMaker(5, 5, 5); | ||
| console.log(cube.volume()); | ||
| console.log(cube.surfaceArea()); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,32 +1,39 @@ | ||
| // ==== Callbacks ==== | ||
| // ==== Callbacks ==== | ||
|
|
||
| /* Step 1: Create a higher-order function that accepts a callback | ||
| * Create a higher-order function named consume that can take 3 parameters. | ||
| * The first two parameters can accept any argument | ||
| * The last parameter accepts a callback | ||
| * In the body of the function return the callback with the two parameters that you created | ||
| */ | ||
|
|
||
| * Create a higher-order function named consume that can take 3 parameters. | ||
| * The first two parameters can accept any argument | ||
| * The last parameter accepts a callback | ||
| * In the body of the function return the callback with the two parameters that you created | ||
| */ | ||
| const consume = (param1, param2, cb) => cb(param1, param2); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. great use of arrow function. pretty neat. |
||
|
|
||
| /* Step 2: Create several functions to callback with consume(); | ||
| * Create a function named add that returns the sum of two numbers | ||
| * Create a function named multiply that returns the product of two numbers | ||
| * Create a function named greeting that accepts a first and last name and returns "Hello first-name last-name, nice to meet you!" | ||
| */ | ||
|
|
||
| * Create a function named add that returns the sum of two numbers | ||
| * Create a function named multiply that returns the product of two numbers | ||
| * Create a function named greeting that accepts a first and last name and returns "Hello first-name last-name, nice to meet you!" | ||
| */ | ||
| function add(param1, param2) { | ||
| return param1 + param2; | ||
| } | ||
| function multiply(param1, param2) { | ||
| return param1 * param2; | ||
| } | ||
| function greeting(frist, last) { | ||
| return `Hello ${frist} ${last}, nice to meet you`; | ||
| } | ||
|
|
||
| /* Step 3: Check your work by un-commenting the following calls to consume(): */ | ||
| // consume(2,2,add); // 4 | ||
| // consume(10,16,multiply); // 160 | ||
| // consume("Mary","Poppins", greeting); // Hello Mary Poppins, nice to meet you! | ||
|
|
||
| consume(2, 2, add); // 4 | ||
| consume(10, 16, multiply); // 160 | ||
| consume("Mary", "Poppins", greeting); // Hello Mary Poppins, nice to meet you! | ||
|
|
||
| // ==== Closures ==== | ||
| // ==== Closures ==== | ||
|
|
||
| // Explain in your own words why `nestedfunction()` can access the variable `internal`. | ||
|
|
||
| // Explanation: | ||
|
|
||
| // myFunction() is called from the Global Scope returning nestedFunction(). | ||
| // nestedFunction() is 'executed' whitin myFunction, thus it has access to myFunction()'s inner and surrounding scopes | ||
| // Explanation: | ||
|
|
||
| const external = "I'm outside the function"; | ||
|
|
||
|
|
@@ -36,7 +43,7 @@ function myFunction() { | |
|
|
||
| function nestedFunction() { | ||
| console.log(internal); | ||
| }; | ||
| } | ||
| nestedFunction(); | ||
| } | ||
| myFunction(); | ||
| myFunction(); | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The super() is used to access and call functions on an object's parent. When used in a constructor, the super keyword appears alone and must be used before the this keyword is used.