Skip to content

Instantly share code, notes, and snippets.

@tobidosumu
Last active December 7, 2022 06:35
Show Gist options
  • Select an option

  • Save tobidosumu/9cd67cfed8b5b6b7797c2e1f4cbf2e4c to your computer and use it in GitHub Desktop.

Select an option

Save tobidosumu/9cd67cfed8b5b6b7797c2e1f4cbf2e4c to your computer and use it in GitHub Desktop.
BackendAssignmentOne
1 ai. List 5 differences between arguments and parameters
Answer:
1. When a function is called, the values that are passed during the call are called as arguments.
The values which are defined at the time of the function prototype or definition of the function are called as parameters.
2. These are used in function call statement to send value from the calling function to the receiving function.
These are used in function header of the called function to receive the value from the arguments.
3. During the time of call each argument is always assigned to the parameter in the function definition.
Parameters are local variables which are assigned value of the arguments when the function is called.
4. They are also called Actual Parameters
They are also called Formal Parameters
5. function user(firstName, lastName) { // firstName and lastName are an parameters
let firstName = "Tobi", lastName = "Dosumu"
console.log(`Hello ${firstName} ${lastName}`)
}
user(firstName, lastName) // firstName and lastName are an arguments
ii. List 5 differences between a function and a method
Answer:
1. Functions have independent existence. You can define them outside of the class.
Methods do not have independent existence. They are always defined within a class, struct, or enum.
2. Functions are the properties of structured languages like C, C++, Pascal and object based language like JavaScript.
Note: There is no concept of function in Java.
Methods are the properties of Object-oriented language like C#, Java, Swift etc.
3. Functions don't have any reference variables.
Methods are called using reference variables.
4. Functions are a self describing piece of code.
Methods are used to manipulate instance variable of a class.
5. Functions are called independently.
Methods are called using instance or object.
iii. What are the differences between a class and a function
Answer:
Functions do specific things, classes are specific things.
Classes often have methods, which are functions that are associated with a particular class,
and do things associated with the thing that the class is - but if all you want is to do something, a function is all you need.
Essentially, a class is a way of grouping functions (as methods) and data (as properties) into a logical unit revolving around
a certain kind of thing. If you don't need that grouping, there's no need to make a class.
bi. List 20 examples of arrow functions with parameters
Answer:
1. let multiply = (a, b) => a * b
2. let divide = (a, b) => a / b
3. let subtract = (a, b) => a - b
4. let add = (a, b) => a + b
5. let modulus = (a, b) => a % b
6. const displacement = (changeInTime, velocity) => changeInTime * velocity
7. const speed = (distance, time) => distance / time
8. const distance = (speed, time) => speed * time
9. const areaOfCircle = (radius) => Math.PI * Math.pow(radius, 2)
10. const velocity = (displacement, changeInTime) => displacement / changeInTime
11. const areaOfTrapezoid = (topBase, bottomBase, height) => (topBase + bottomBase) / 2 * height
12. const acceleration = (finalVelocity, initialVelocity, elapsedTime) => (finalVelocity - initialVelocity) / elapsedTime
13. const changeInVelocity = (acceleration, elapsedTime) => acceleration * elapsedTime
14. const areaOfParallelogram = (base, height) => base * height
15. const areaOfRectangle = (width, length) => width * length
16. const areaOfTriangle = (height, base) => height * base / 2
17. const areaOfSquare = (side) => Math.pow(side, 2)
18. const areaOfCylinder = (radius, height) => 2 * Math.PI * radius * height + 2 * Math.PI * Math.pow(radius, 2)
19. const mentorsInfo = (mentorJeph, mentorMike) => `My mentors are ${mentorJeph} and ${mentorMike}`
20. let message = (language) => `I'm learning ${language}`
bii. List 20 examples of arrow functions with arguments
Answer:
1. let multiply = (a, b) => a * b
console.log(multiply(2, 7))
2. let divide = (a, b) => a / b
console.log(divide(10, 2))
3. let subtract = (a, b) => a - b
console.log(subtract(10, 3))
4. let add = (a, b) => a + b
console.log(add(4, 3))
5. let modulos = (a, b) => a % b
console.log(modulos(14, 2))
6. const displacement = (changeInTime, velocity) => changeInTime * velocity
console.log(displacement(4, 3))
7. let speed = (distance, time) => distance/time
console.log(speed(4, 7))
8. let distance = (speed, time) => speed * time
console.log(distance(3, 7))
9. const areaOfCircle = (radius) => Math.PI * Math.pow(radius, 2)
console.log(areaOfCircle(7))
10. const velocity = (displacement, changeInTime) => displacement/changeInTime
console.log(velocity(10, 4))
11. const areaOfTrapezoid = (topBase, bottomBase, height) => (topBase + bottomBase) / 2 * height
console.log(areaOfTrapezoid(7, 18, 14))
12. const acceleration = (finalVelocity, initialVelocity, elapsedTime) => (finalVelocity - initialVelocity) / elapsedTime
console.log(acceleration(12, 2, 7))
13. const changeInVelocity = (acceleration, elapsedTime) => acceleration * elapsedTime
console.log(changeInVelocity(2, 8))
14. const areaOfParallelogram = (base, height) => base * height
console.log(areaOfParallelogram(12, 8))
15. const areaOfRectangle = (width, length) => width * length
console.log(areaOfRectangle(5, 10))
16. const areaOfTriangle = (height, base) => height * base / 2
console.log(areaOfTriangle(14, 20))
17. const areaOfSquare = (side) => Math.pow(side, 2)
console.log(areaOfSquare(4))
18. const areaOfCylinder = (radius, height) => 2 * Math.PI * radius * height + 2 * Math.PI * Math.pow(radius, 2)
console.log(areaOfCylinder(4, 14))
19. const mentorsInfo = (mentorJeph, mentorMike) => `My mentors are ${mentorJeph} and ${mentorMike}`
console.log(mentorsInfo('Mr. Jeph', 'Mr. Mike'))
20. let message = (language) => `I'm learning ${language}`
console.log(message("ExpressJs"))
c. List 10 differences between arrow functions and normal function
Answer:
1. Syntax:
A programmer can get the same result as regular functions by writing a few lines of code using arrow functions.
Curly brackets are not required if only one expression is present.
// Regular function ES5:
var add = function(a, b) { return a + b;};
// Arrow function ES6
let add = (a, b) => { return a + b};
//or
let add = (a, b) => a + b;
2. Arguments binding:
arguments object inside the regular functions contains the list of arguments.
// Object with Regular function
let showData = {
showArg: function(){
console.log(arguments);
}
}
showData.showArg(1,2,3); // output {0:1,1:2,2:3}
The arrow function, on the opposite, doesn’t define arguments i.e. they do not have arguments binding.
// Object with Arrow function
let showData = {
showArg: ()=>console.log(arguments);
}
showData.showArg(1,2,3);
// Uncaught ReferenceError: arguments is not defined
But you can easily access the arrow function arguments using a rest parameter ...args.
// using rest parameters
let showData = {
showArg: (...args)=>console.log(args);
}
myFunc.showArgs(1, 2, 3, 4); // [1, 2, 3, 4]
3. Use of this keyword
Inside of a regular JavaScript function, this value is dynamic. The dynamic context means that the value of this
depends on how the function is invoked.
let name ={
fullName:'abc',
printInRegular: function(){
console.log(`My Name is ${this.fullName}`);
},
printInArrow:()=>console.log(`My Name is ${this.fullName}`)
}
name.printInRegular(); // My Name is abc
name.printInArrow(); // My Name is undefined
The behavior of this inside of an arrow, function differs considerably from the regular function’s this behavior
as an arrow function does not have its own “this” keyword.
The value of this inside an arrow function remains the same throughout the lifecycle of the function and is always
bound to the value of this in the closest non-arrow parent function which means No matter how or where being executed,
this value inside of an arrow function always equals this value from the outer function.
const myObject = {
myMethod(items) {
console.log(this); // logs myObject
const callback = () => {
console.log(this); // this takes value from myMethod(outer func)
};
items.forEach(callback);
}
};
myObject.myMethod([1, 2, 3]);
4. Using a new keyword
Regular functions are constructible and callable. They can be called using the new keyword.
function Car(color) {
this.color = color;
}
const redCar = new Car('red');
redCar instanceof Car; // => true
But, the arrow functions are only callable and not constructible, i.e., arrow functions can never be used as constructor
functions.
const Car = (color) => {
this.color = color;
};
const redCar = new Car('red'); // TypeError: Car is not a constructor
5. No duplicate named parameters
Arrow functions can never have duplicate named parameters, whether in strict or non-strict mode.
It means that the following is valid JavaScript:
function add(x, x){}
It is not, however, when using strict mode:
'use strict';
function add(x, x){}
// SyntaxError: duplicate formal argument x
With arrow functions, duplicate named arguments are always, regardless of strict or non-strict mode, invalid.
(x, x) => {}
// SyntaxError: duplicate argument names not allowed in this context
6. Return value can omit the curly brackets
function traditionalFn() {
return Math.max(...arguments);
}
const arrowFn = (...rest) => Math.max(...rest);
You can see that in an inline arrow function that contains only one expression, we can omit the curly
braces to return the value, which makes the code clearer.
7. No prototype
function traditionalFn() {
return Math.max(...arguments);
}
const arrowFn = (...rest) => {
return Math.max(...rest);
};
// {constructor: ƒ traditionalFn()}
console.log(traditionalFn.prototype);
// undefined
console.log(arrowFn.prototype);
We can get prototype for traditional function, but the arrow function does not have prototype.
8. No this
In a traditional function, its internal this value is dynamic, it depends on how the function is invoked. For example:
const test = {
name: 1,
getName: function () {
return this.name;
},
};
const getName = test.getName;
console.log(getName()); // undefined
console.log(test.getName()); // 1
In the arrow function, there are no this, if we access this in the arrow function it will return
the this of the closest non-arrow parent function.
globalThis.name = 2;
const test = {
name: 1,
getName: () => {
return this.name;
},
};
const getName = test.getName;
console.log(getName()); // 2
console.log(test.getName()); // 2
Note that the this of an arrow function is determined at the time of declaration and never changes. So call, apply,
bind cannot change the value of arrow function this.
9. Cannot be invoked with new
We can use the new keyword on the traditional function to create a new object.
function Animal(name) {
this.name = name;
}
const cat = new Animal('cat');
// Animal { name: 'cat' }
console.log('cat: ', cat);
But arrow functions cannot be called with new.
const Animal = (name) => {
this.name = name;
};
// ❌ TypeError: Animal is not a constructor.
const cat = new Animal('cat');
This is because when calling new, we go through the following four steps:
Create a new object
Point the __proto__ of the new object to the prototype of the constructor
Call the constructor with the new object as this
If the result of the call is an object, return the object, if not, return the new object created in the first step.
We can implement a mock new function:
const _new = (fn, ...args) => {
const newObj = Object.create(null);
Object.setPrototypeOf(newObj, fn.prototype);
const callResult = fn.apply(newObj, args);
return typeof callResult === 'object' ? callResult : newObj;
};
So you can see that the arrow function cannot be called by the new keyword because it has no prototype and no this.
10. Cannot be used as a Generator function
For historical reasons, the specification does not allow the use of the yield command in the arrow function, so the arrow function cannot be used as a Generator function.
d. List 10 differences between arrow functions and normal functions asides from those in c
Answer:
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
2 a. Read the express generator page
b. Follow the instructions and create a second application
c. change the port to another port number
d. Push to GitHub (link to second app https://github.com/tobidosumu/my-second-api.git)
e. Add mr mike as a collaborator
f. Add an env
g. Push the first API to GitHub (link to first app https://github.com/tobidosumu/my-first-api.git)
h. Add Mr. Mike as a collaborator
i. Read on Node global objects
j. Use Node global object for 5 variables
k. Push to GitHub
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment