{"id":20493648,"url":"https://github.com/code-lucidal58/javascript_leetcode","last_synced_at":"2025-03-05T17:44:59.840Z","repository":{"id":163962663,"uuid":"639396495","full_name":"code-lucidal58/javascript_leetcode","owner":"code-lucidal58","description":"Notes for javascript through leetcode challenges","archived":false,"fork":false,"pushed_at":"2023-05-15T16:08:00.000Z","size":8,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-01-16T05:55:45.651Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/code-lucidal58.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-05-11T11:46:37.000Z","updated_at":"2023-05-11T12:25:54.000Z","dependencies_parsed_at":null,"dependency_job_id":"bd9651ae-d98f-4599-afb8-79fcbaaa5df4","html_url":"https://github.com/code-lucidal58/javascript_leetcode","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/code-lucidal58%2Fjavascript_leetcode","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/code-lucidal58%2Fjavascript_leetcode/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/code-lucidal58%2Fjavascript_leetcode/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/code-lucidal58%2Fjavascript_leetcode/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/code-lucidal58","download_url":"https://codeload.github.com/code-lucidal58/javascript_leetcode/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":242075272,"owners_count":20068224,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-15T17:36:06.036Z","updated_at":"2025-03-05T17:44:59.817Z","avatar_url":"https://github.com/code-lucidal58.png","language":"JavaScript","readme":"n=n+1# JavaScript Leetcode\nLeetcode started LC Javascript Challenge in May 2023. This is the list of all problems that were asked each day during the time.\n* [Day 1](#day-1)\n* [Day 2](#day-2)\n* [Day 3](#day-3)\n* [Day 4](#day-4)\n* [Day 5](#day-5)\n* [Day 6](#day-6)\n* [Day 7](#day-7)\n* [Day 8](#day-8)\n* [Day 9](#day-9)\n* [Day 10](#day-10)\n\n***Javascript Guide***: [MDN webdocs](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide)\n## Day 1\nCode: [Create Hello World Function](./create_hello_world_function.js)\n\n### Functions\nFunction can be defined using the word `function`.\n#### Basic Syntax\n```js\nfunction f(a, b) {\n  const sum = a + b;\n  return sum;\n}\nconsole.log(f(3, 4)); // 7\n```\n\n#### Anonymous Syntax\n```js\nvar f = function(a, b) {\n  const sum = a + b;\n  return sum;\n}\nconsole.log(f(3, 4)); // 7\n```\n\n#### Immediately Invoked Function Expression (IIFE)\n```js\nconst result = (function(a, b) {\n  const sum = a + b;\n  return sum;\n})(3, 4);\nconsole.log(result); // 7\n```\n\n#### Functions with Functions\n```js\nfunction createFunction() {\n  function f(a, b) {\n    const sum = a + b;\n    return sum;\n  }\n  return f;\n}\nconst f = createFunction();\nconsole.log(f(3, 4)); // 7\n```\n\n#### Function Hoisting\nA function can be used before it is initialized. Only done when declaration of functions is with the `function` (basic) syntax. However, this is considered bad practice as it reduces readability.\n```js\nfunction createFunction() {\n  return f;\n  function f(a, b) {\n    const sum = a + b;\n    return sum;\n  }\n}\nconst f = createFunction();\nconsole.log(f(3, 4)); // 7\n```\n\n#### Closures\nWhen a function is created, it has access to a reference to all the variables declared around it (in the scope), also known as it's `lexical environment`. The combination of the function and its environment is called a `closure`.\n```js\nfunction createAdder(a) {\n  function f(b) {\n    const sum = a + b;\n    return sum;\n  }\n  return f;\n}\nconst f = createAdder(3);\nconsole.log(f(4)); // 7\n```\nIn this example, `createAdder` passes the first parameter `a` and the inner function has access to it. This way, `createAdder` serves as a factory of new functions, each time it is called with a different value of `a`. The returned function `f` will act differently each time.\n\n### Arrow syntax\nIt is a preferred way of declaring a function. These cannot be used as constructors. Calling them with `new` will throw `TypeError`. They do not support `yield`, hence cannot be generator functions.\n#### Basic Syntax\n```js\nconst f = (a, b) =\u003e {\n  const sum = a + b;\n  return sum;\n};\nconsole.log(f(3, 4)); // 7\n```\n\n#### Omit Return\nIf function statement is very small, then `return` statement can be omitted.\n```js\nconst f = (a, b) =\u003e a + b;\nconsole.log(f(3, 4)); // 7\n```\n\n### Rest Arguments\n`Spread`(...) allows an iterable to be expanded inplace.\n```js\nfunction f(...args) {\n  const sum = args[0] + args[1];\n  return sum;\n}\nconsole.log(f(3, 4)); // 7\n```\nThis can be used to create generic function factory.\n```js\nfunction log(inputFunction) {\n  return function(...args) {\n     console.log(\"Input\", args);\n     const result = inputFunction(...args);\n     console.log(\"Output\", result);\n     return result;\n  }\n}\nconst f = log((a, b) =\u003e a + b);\nf(1, 2); // Logs: Input [1, 2] Output 3\n```\n\n## Day 2\nCode: [Counter](./counter.js)\n\nDiscussed earlier, a function has reference to all variables inside it and any other variable/function in its outer scope. This is called lexical environment. This promotes `encapsulation`. Each time the outer function is called, a separate copy of the function statements and variables inside it is maintained.\n\n## Day 3\nCode: [Counter II](./counter_ii.js)\n\n### JavaScript Objects\nObjects are strings mapped to other objects. The string is called the key. The value can be any object, i.e., string, boolean, other object etc.\n```js\nconst d = {\n  \"num\": 1,\n  \"str\": \"Hello World\",\n  \"obj\": {\n    \"x\": 5\n  }\n};\n```\nThere are three ways of accessing the object items:\n* **Dot Notation**: `d.obj.x // 5`\n* **Bracket Notation**: `d[\"obj\"][\"x\"] // 5`\n* **Destructuring Notation**: `const {num, str} = d // 1 \"Hello World\"` This syntax is used to access multiple values in one go. The name of the variables should be same as the key name in the object. The variables can be declared using any keyword of variable declaration.\n\n### Classes and Prototypes\n```js\nclass Person {\n  constructor(name, age) {\n    this.name = name;\n    this.age = age;\n  }\n\n  greet() {\n    console.log(\"My name is\", this.name);\n  }\n}\n\nconst alice = new Person(\"Alice\", 25);\nalice.greet(); // Logs: \"My name is Alice\"\n```\nThis is an example of a class. The constructor creates an object. Every time an instance of the class is created, a copy of the function method is also created. An efficient way of the above can be having just a single copy of the class method. This is possible if the function is a `prototype`.\n```js\nconst alice = {\n  name: \"Alice\",\n  age: 25,\n  __proto__: {\n    greet: function() {\n      console.log(\"My name is\", this.name);\n    }\n  }\n};\nalice.greet(); // Logs: \"My name is Alice\"\n```\nJavaScript will first search for the function `greet`. If it cannot find it, it will search it in the function's prototype. Then, it will search in the prototype's prototype, and so on. This is how **inheritance** works. Irrespective of the number of instances of the class are created, only single prototyped object is created.\n\n### Proxies\nProxy is a powerful feature that allows overriding the default behaviour of objects. It allows you to create an object that can be used in place of the original object, but which may redefine fundamental Object operations like getting, setting, and defining properties.\n```js\nconst alice = new Proxy({name: \"Alice\", age: 25}, {\n  get: (target, key) =\u003e {\n    if (key === 'greet') {\n      return () =\u003e console.log(\"My name is\", target.name);\n    } else {\n      return target[key];\n    }\n  },\n});\nalice.greet(); // Logs: \"My name is Alice\"\n```\nIt has two parameters:\n* **target**: the original object which you want to proxy\n* **handler**: an object that defines which operations will be intercepted and how to redefine intercepted operations.\n\nSome of the usages are as listed below:\n* Perform validation\n```js\nconst validator = {\n  set: (obj, prop, value) =\u003e {\n    if (prop === \"age\") {\n      if (typeof value !== \"number\" || value \u003c 0) {\n        throw new TypeError(\"Age must be a positive number\");\n      }\n    }\n    obj[prop] = value;\n  },\n};\n\nconst person = new Proxy({}, validator);\nperson.age = 25; // Works fine\nperson.age = -5; // Throws an error\n```\n* Create logs when a key is accessed\n```js\nconst object = {\n  \"num\": 1,\n  \"str\": \"Hello World\",\n  \"obj\": {\n    \"x\": 5\n  }\n};\nconst proxiedObject = new Proxy(object, {\n  get: (target, key) =\u003e {\n    console.log(\"Accessing\", key);\n    return target[key];\n  }\n});\nproxiedObject.num; // Logs: Accessing num\n```\n* Throw error when an attempt to write a read-only value is made\n```js\nconst READONLY_KEYS = ['name'];\n\nconst person = new Proxy({ name: \"Alice\", age: 25 }, {\n  set: (target, key, value) =\u003e {\n    if (READONLY_KEYS.includes(key)) {\n      throw Error(\"Cannot write to key\");\n    }\n    target[key] = value;\n    return true;\n  }\n});\nperson.name = \"Bob\"; // Throws Error\n```\n* Create a modified version of an immutable object by writing to it's proxy. This is implemented with the popular library `immer`.\n\n## Day 4\nCode: [Apply Transform Over Each Element in Array](apply-transform-over-each-element-in-array.js)\n\nArrays can be declared and accessed in a number of ways.\n```js\nconst newArr = [];\nfor (let i = 0; i \u003c arr.length; ++i) {\n    newArr[i] = i; // or newArr.push(i);\n}\n\nconst newArr1 = new Array(newArr.length);https://www.w3schools.com/jsref/jsref_map.asp\n\nconst newArr = new Int32Array(arr.length);\n```\n### Callbacks\nA callback is defined as a function passed as an argument to another function. It is used to write code that can be reused across different use-cases. One simple example is `Array.map`. It creates a new array from calling a function on every array element. It does not execute on empty elements and does not change the original array.\n```js\n// The following code squares each element of the array and create a new array with the result\nconst numbers = [4, 9, 16, 25];\nconst newArr = numbers.map(Math.sqrt);\n```\n\n## Day 5\nCode: [Function Composition](function_composition.js)\nTODO\n\n## Day 6\nCode: [Function Composition](function_composition.js)\nTODO\n\n## Day 7\nCode: [Function Composition](function_composition.js)\n\n`Function composition`is a concept in functional programming where the output of one function is used as the input of another function. In other words, it's the process of chaining two or more functions together so that the result of one function becomes the input to the next.\nThe notation (f ∘ g)(x) is used in mathematics to represent function composition. It is read as \"f composed with g\" or \"f of g\", or f(g(x)).\n\n## Day 8\nCode: [Function Composition](function_composition.js)\nTDOD\n\n## Day 9\nCode: [Function Composition](function_composition.js)\nTDOD\n\n## Day 10\nCode: [Function Composition](function_composition.js)\nTDOD\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcode-lucidal58%2Fjavascript_leetcode","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcode-lucidal58%2Fjavascript_leetcode","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcode-lucidal58%2Fjavascript_leetcode/lists"}