diff --git a/src/data/question-groups/javascript/content/prototype-chain.md b/src/data/question-groups/javascript/content/prototype-chain.md new file mode 100644 index 000000000..bde4a9fb8 --- /dev/null +++ b/src/data/question-groups/javascript/content/prototype-chain.md @@ -0,0 +1,27 @@ +The prototype chain in JavaScript refers to the chain of objects linked by their prototypes. When a property or method is accessed on an object, JavaScript first checks the object itself. If it doesn't find it there, it looks up the property or method in the object's prototype. This process continues, moving up the chain from one prototype to the next, until the property or method is found or the end of the chain is reached (typically the prototype of the base object, which is `null`). The prototype chain is fundamental to JavaScript's prototypal inheritance model, allowing objects to inherit properties and methods from other objects. + +## Example + +```js +const roadmap = { + getRoadmapUrl() { + console.log(`https://roadmap.sh/${this.slug}`); + }, +}; + +const javascript = { + name: 'JavaScript Roadmap', + description: 'Learn JavaScript', + slug: 'javascript', + greet() { + console.log(`${this.name} - ${this.description}`); + }, +}; + +Object.setPrototypeOf(javascript, roadmap); // or javascript.__proto__ = roadmap; + +javascript.getRoadmapUrl(); // https://roadmap.sh/javascript +javascript.greet(); // JavaScript Roadmap - Learn JavaScript +``` + +In the above example, the `javascript` object inherits the `getRoadmapUrl()` method from the `roadmap` object. This is because the `javascript` object's prototype is set to the `roadmap` object using the `Object.setPrototypeOf()` method. In the `javascript` object, the `getRoadmapUrl()` method is not found, so JavaScript looks up the prototype chain and finds the `getRoadmapUrl()` method in the `roadmap` object. diff --git a/src/data/question-groups/javascript/content/reduce-method.md b/src/data/question-groups/javascript/content/reduce-method.md new file mode 100644 index 000000000..c42f1b87d --- /dev/null +++ b/src/data/question-groups/javascript/content/reduce-method.md @@ -0,0 +1,24 @@ +You can use the `reduce()` method to reduce an array to a single value. The `reduce()` method executes a reducer function (that you provide) on each element of the array, resulting in a single output value. + +## Syntax + +```js +array.reduce((accumulator, currentValue) => { + // ... +}, initialValue); +``` + +## Example + +You can use the `reduce()` method to sum all the numbers in an array. + +```js +const numbers = [1, 2, 3, 4, 5, 6]; + +const sum = numbers.reduce((accumulator, currentValue) => { + return accumulator + currentValue; +}, 0); + +console.log(numbers); // [1, 2, 3, 4, 5, 6] +console.log(sum); // 21 +``` diff --git a/src/data/question-groups/javascript/javascript.md b/src/data/question-groups/javascript/javascript.md index 215e65827..6f538385b 100644 --- a/src/data/question-groups/javascript/javascript.md +++ b/src/data/question-groups/javascript/javascript.md @@ -88,4 +88,19 @@ questions: topics: - 'Core' - 'Intermediate' + - question: How to use `reduce()` method? + answer: reduce-method.md + topics: + - 'Core' + - 'Intermediate' + - question: What is the difference between `map()` and `reduce()` methods? + answer: The `map()` method creates a new array with the results of calling a provided function on every element in the calling array. Whereas, the `reduce()` method executes a reducer function (that you provide) on each element of the array, resulting in a single output value. + topics: + - 'Core' + - 'Intermediate' + - question: What is Prototype Chain in JavaScript? + answer: prototype-chain.md + topics: + - 'OOP' + - 'Advanced' ---