Created
September 20, 2025 17:33
-
-
Save davidystephenson/63fbbe01c013b57cb825c9a055443436 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var person = { | |
firstName: 'Zelda', | |
lastName: 'Fitzgerald', | |
introduce: function () { | |
console.log('my name is ' + this.firstName + ' ' + this.lastName) | |
} | |
} | |
// Question 1: Given a greet() function modify it to update its context with an object | |
// having firstName and lastName properties. | |
// function greet(name){ | |
// console.log("Hi " + name + ', my name is ' + this.firstName + ' ' + this.lastName); | |
// } | |
// greet("Ron"); | |
// greet.call(person, 'Ron') | |
// Question 2: Given a greet() function modify it to update its context with an object | |
// having firstName and lastName properties | |
// and allow it to accept multiple arguments in an array | |
// function greet(name, lastName){ | |
// console.log("Hi " + name + " " + lastName + ' my name is ' + this.firstName + ' ' + this.lastName); | |
// } | |
// greet("Ron", "Weasley"); | |
// var array = ['Ron', 'Weasley'] | |
// greet.apply(person, array) | |
// Question 3: Given a greet() function modify it | |
// to update its context with a fixed value so it can be invoked later. | |
/* | |
function greet(name){ | |
console.log("Hi" + name + this + "is the original context of the function"); | |
} | |
greet("Ron"); | |
*/ | |
// function greet (name) { | |
// console.log('Hi ' + name + ' I am ' + this.firstName) | |
// } | |
// var bound = greet.bind(person, 'Ron') | |
// bound() | |
// bound() | |
// bound() | |
// Question 4: Create a function multiplyBy that multiplies a number by a given factor. | |
// Use the bind method to create a new function that multiplies by a specific factor. | |
// function multiplyBy (a, b) { | |
// console.log(a * b) | |
// } | |
// multiplyBy(5, 10) | |
// var multiplyByTen = multiplyBy.bind(undefined, 10) | |
// multiplyByTen(3) | |
// multiplyByTen(4) | |
// function multiplyBy5 (a) { | |
// multiplyBy(a, 5) | |
// } | |
// multiplyBy5(5) | |
// Question 5: Create a function that counts the number of properties in an object | |
// using `apply` and the `arguments` object. | |
function count (object) { | |
console.log(Object.keys(object).length) | |
} | |
// count(person) | |
count.apply(undefined, [person]) | |
// Question 6: Given an object `person` representing a person with a method to introduce himself, | |
// define another object `anotherPerson` and using call method borrow the introduce method from `person` object | |
// and use it for `anotherPerson` object | |
person.introduce() | |
var anotherPerson = { | |
firstName: 'Dorothy', | |
lastName: 'Parker' | |
} | |
person.introduce.call(anotherPerson) | |
// Question 7: Using the `apply` method, write a function that finds the minimum number in an array. | |
// You can use Math class's built-in min() method for this task. | |
function findMinimum (numbers) { | |
// Math.min(1, 2, 3) | |
// var result = Math.min.apply(null, numbers) | |
// console.log(result) | |
// console.log(Math.min(...numbers)) | |
} | |
findMinimum([3, 1, -2, 100, -3]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment