{"id":21700862,"url":"https://github.com/srivenkat13/js-practice","last_synced_at":"2026-05-17T03:45:16.971Z","repository":{"id":44358771,"uuid":"510791094","full_name":"srivenkat13/js-practice","owner":"srivenkat13","description":"All the JS mini projects and practice exercises ","archived":false,"fork":false,"pushed_at":"2024-08-29T12:44:16.000Z","size":62,"stargazers_count":0,"open_issues_count":0,"forks_count":1,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-10-19T19:50:04.625Z","etag":null,"topics":["dom","dom-manipulation","javascript","scrimba"],"latest_commit_sha":null,"homepage":"https://srivenkat13.github.io/js-practice/","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/srivenkat13.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":"2022-07-05T15:21:26.000Z","updated_at":"2024-08-29T12:44:20.000Z","dependencies_parsed_at":"2025-03-20T15:44:32.089Z","dependency_job_id":"11cbd3b0-8ff8-45d0-a2dd-d302f8cec920","html_url":"https://github.com/srivenkat13/js-practice","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/srivenkat13/js-practice","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/srivenkat13%2Fjs-practice","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/srivenkat13%2Fjs-practice/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/srivenkat13%2Fjs-practice/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/srivenkat13%2Fjs-practice/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/srivenkat13","download_url":"https://codeload.github.com/srivenkat13/js-practice/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/srivenkat13%2Fjs-practice/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":33127001,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-05-16T18:38:32.183Z","status":"online","status_checked_at":"2026-05-17T02:00:05.366Z","response_time":107,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"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":["dom","dom-manipulation","javascript","scrimba"],"created_at":"2024-11-25T20:16:58.270Z","updated_at":"2026-05-17T03:45:16.953Z","avatar_url":"https://github.com/srivenkat13.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"\u003cdetails\u003e\u003csummary\u003eTable of Contents\u003c/summary\u003e\n\n \u003ch2\u003eTable of Contents \u003c/h2\u003e\n\n- [Polyfills](#polyfills)\n  - [1. map](#1-map)\n  - [2. Filter](#2-filter)\n  - [3. Reduce](#3-reduce)\n  - [4. Call Bind Apply](#4-call-bind-apply)\n- [Important JS Questions](#important-js-questions)\n  - [1. Implement Fetch with n retries](#1-implement-fetch-with-n-retries)\n  - [2. Implement Memoize](#2-implement-memoize)\n  - [3. Implement Debounce](#3-implement-debounce)\n  - [4. Execute async functions in Series](#4-execute-async-functions-in-series)\n- [Objects](#objects)\n  - [1. Transform the given input to output](#1-transform-the-given-input-to-output)\n  - [2. Nested Object](#2-nested-object)\n  - [3. Accessing Object Properties](#3-accessing-object-properties)\n  - [4. Merging Objects](#4-merging-objects)\n  - [5. Flattening a Nested Object](#5-flattening-a-nested-object)\n  - [6. Merging and Summarizing Array of Objects](#6-merging-and-summarizing-array-of-objects)\n  - [7. Checking Object Equality](#7-checking-object-equality)\n  - [8. Counting Properties in an Object](#8-counting-properties-in-an-object)\n  - [9. Swap keys to Values in an object](#9-swap-keys-to-values-in-an-object)\n- [Arrays](#arrays)\n  - [1. Sum of min-max](#1-sum-of-min-max)\n  - [2. Two Sum](#2-two-sum)\n- [Strings](#strings)\n  - [1. Can form Palindrome](#1-can-form-palindrome)\n  - [2. Check if Anagram](#2-check-if-anagram)\n  - [3. Non repeating Character](#3-non-repeating-character)\n  - [4. Find the most repeated character](#4-find-the-most-repeated-character)\n\u003c/details\u003e\n\n### Polyfills\n#### 1. map\n\n```js\nArray.prototype.myMap = function (cb) {\n    let result =[]\n    for(let i = 0; i \u003c this.length ; i++) {\n        result.push(cb(this[i],i,this))\n    }\n    return result\n}\n```\n\n#### 2. Filter\n```js\nArray.prototype.myFilter = function (cb){\n    \n    let result = []\n    for (let i = 0; i \u003cthis.length; i++){\n        if(cb(this[i],i,this)) {\n            result.push(this[i])\n        }\n    }\n    return result\n}\n```\n\n#### 3. Reduce\n```js\nArray.prototype.myReduce = function (cb,init) {\n    let acc = init\n    for (let i = 0; i \u003c this.length; i++) {\n        acc = acc? cb(acc,this[i],i,this)  : this[i]      \n    }\n    return acc\n}\n```\n#### 4. Call Bind Apply\n\u003cdetails\u003e\u003csummary\u003e.call()\u003c/summary\u003e\n\u003cp\u003e\n\n```js\nFunction.prototype.MyCall = function (context = {}, ...args) {\n  if (typeof this !== \"function\") throw new Error(`${this} is not a function`);\n  //if its not called on an Object\n  if (typeof context !== \"object\") context = Object(context);\n\n  context.fn = this;\n  context.fn(...args);\n};\nfunction greet(greeting, punctuation) {\n  console.log(greeting + \", \" + this.name + punctuation);\n}\nconst user = { name: \"venkat\" };\ngreet.MyCall(user,'Hello','!') //Hello, venkat!\n```\n\u003c/p\u003e\n\u003c/details\u003e\n\n\u003cdetails\u003e\u003csummary\u003e.apply()\u003c/summary\u003e\n\u003cp\u003e\n\n```js\nFunction.prototype.MyApply = function (context = {}, ...args) {\n  if (typeof this !== \"function\") throw new Error(`${this} is not a function`);\n   if (!Array.isArray(args)) {\n    throw new Error(`CreateListFromArrayLike called on non-object`);\n  }\n\n  context.fn = this;\n  context.fn(...args);\n};\ngreet.MyApply(user,'Hello','!') //Hello, venkat!\n```\n\u003c/p\u003e\n\u003c/details\u003e\n\n\u003cdetails\u003e\u003csummary\u003e.bind()\u003c/summary\u003e\n\u003cp\u003e\n\n```js\nFunction.prototype.MyBind = function (context = {}, ...args) {\n  if (typeof this !== \"function\") throw new Error(`${this} is not a function`);\n  \n  context.fn = this;\n  return function(...newArgs) {\n    context.fn(...args,...newArgs);\n  }\n};\nconst newGreet = greet.MyBind(user,'Hello','!') \nnewGreet() //Hello, venkat!\n```\n\u003c/p\u003e\n\u003c/details\u003e\n\n ### Important JS Questions\n #### 1. Implement Fetch with n retries \n\n \u003cdetails\u003e\u003csummary\u003e\u003cb\u003eSolution\u003c/b\u003e\u003c/summary\u003e\n\u003cp\u003e\nwe introduce pause between each retry using a promise\n\n```js\nasync function fetchWithRetries (url, options= {} , retries , delay) {\n  for (let i =1 ; i\u003cretries ; i++){\n    try{\n      const response = await fetch (url,options)\n      if(!response.ok) {\n        throw new Error(`HTTP Error ${response.status}`)\n      }\n      return response\n    }\n    catch(error){\n      if(i\u003creries) console.log(`Retry... ${i}`)\n      else throw error\n    await new Promise (resolve =\u003e setTimeOut(resolve,delay))\n    }\n  }\n}\nconst url_working=\"https://jsonplaceholder.typicode.com/posts/1\"\nconst url_not_working=\"https://jsonplaceholder.typocode.com/posts/1\"\n\nfetchWithRetry(url_not_working, {},5, 2000)\n  .then((response) =\u003e response.json())\n  .then((data) =\u003e console.log(data))\n  .catch((error) =\u003e console.error(\"Fetch failed:\", error));\n\n```\n\n\u003c/p\u003e\n\u003c/details\u003e\n\n #### 2. Implement Memoize\n\n \u003cdetails\u003e\u003csummary\u003e\u003cb\u003eSolution\u003c/b\u003e\u003c/summary\u003e\n\u003cp\u003e\n\n\n```js\nfunction Memoize (fn) {\n  const cache={}\n  return function (...args) {\n    let cacheArgs = JSON.stringify(args)\n    if(!cache.hasOwnProperty(cacheArgs)){\n      cache[cacheArgs] = fn(...args))\n    }\n    return cache[cacheArgs]\n  }\n}\nfunction messyProd(num1, num2) {\n  for (let i = 0; i \u003c 2000000000; i++) {}\n  return num1 * num2;\n}\nconst memoizedMessyProd = Memoize(messyProd);\n\nconsole.time(\"First\");\nmemoizedMessyProd(134, 345);\nconsole.timeEnd(\"First\");\n\nconsole.time(\"Second\");\nmemoizedMessyProd(135, 345);\nconsole.timeEnd(\"Second\");\n\nconsole.time(\"Third\");\nmemoizedMessyProd(135, 345);\nconsole.timeEnd(\"Third\");\n//First: 1.610s\n//Second: 0.039ms\n//Third: 0.02ms\n\n```\n\n\u003c/p\u003e\n\u003c/details\u003e\n \n #### 3. Implement Debounce\n \u003cdetails\u003e\u003csummary\u003e\u003cb\u003eSolution\u003c/b\u003e\u003c/summary\u003e\n\u003cp\u003e\n\n```js\nconst debounce = (fn, delay) =\u003e {\n  let inDebounce;\n  return function () {\n    const context = this;\n    const args = arguments;\n    clearTimeout(inDebounce);\n    inDebounce = setTimeout(() =\u003e fn.apply(context, args), delay);\n  };\n};\n\nconst greet = () =\u003e {\nconsole.log('hey');\n}\nconst debouncedGreet = debounce(greet, 500);\nwindow.addEventListener('mousemove', debouncedGreet);\n\n```\n\n\u003c/p\u003e\n\u003c/details\u003e\n \u003cdetails\u003e\u003csummary\u003e\u003cb\u003eWith Immediate Flag\u003c/b\u003e\u003c/summary\u003e\n\u003cp\u003e\n\n```js\nconst debounceWithImmediate = (func, wait, immediate) =\u003e {\n  let timeout;\n  return function () {\n    let context = this;\n    let args = arguments;\n\n    const callNow = immediate \u0026\u0026 !timeout;\n    clearTimeout(timeout);\n\n    timeout = setTimeout(function () {\n      timeout = null;\n      if (!immediate) {\n        func.apply(context, args);\n      }\n    }, wait);\n    if (callNow) func.apply(context, args);\n  };\n};\n\nconst greet = () =\u003e {\nconsole.log('hey');\n}\nconst debouncedGreet = debounceWithImmediate(greet, 500,true);\nwindow.addEventListener('mousemove', debouncedGreet);\n\n```\n\n\u003c/p\u003e\n\u003c/details\u003e\n \n#### 4. Execute async functions in Series\n\u003cdetails\u003e\u003csummary\u003e\u003cb\u003eSolution\u003c/b\u003e\u003c/summary\u003e\n\u003cp\u003e\n\n```js\nconst asyncSerialExecuter = async function(promises) {\n  for(let prom of promises){\n    try{\n      const result = await prom\n      console.log(result)\n    }\n    catch(e){\n      console.log(e)\n    }\n  }\n}\n//using recurion\nconst asyncSerialExecuter2 = function(promises){\n  let promise = promises.shift()\n  promise.then(data=\u003e \n  console.log(data))\n\n  if(promises.length \u003e 0 ){\n    asyncSerialExecuter2(promises)\n  }\n}\n\nconst asyncTask = function (i){\n  return new Promise((resolve,reject)=\u003e{\n    setTimeout(()=\u003eresolve(`Completing ${i}`),1000*i)\n  })\n}\n\n\nconst promises =[\n  asyncTask(1),\n  asyncTask(3),\n  asyncTask(4),\n  asyncTask(6),\n  asyncTask(2)\n]\n\nasyncSerialExecuter(promises)\n//Completing 1\n//Completing 3\n//Completing 4\n//Completing 6\n//Completing 2\n\n```\n\u003c/p\u003e\n\u003c/details\u003e\n \n###  Objects\n#### 1. Transform the given input to output\n\n```js\nconst input = {\n    array: [1, 34, 3, 7, 8, 9],\n    object: {a: 1, b: 3, b: {d: 4, e: 5}},\n    string: ['whatever']\n};\n```\n\n```js\n{\n  array: [1, 3, 7, 8, 9, 34],\n  object: [\"a\", \"b\", \"d\", \"e\"],\n  string: [[\"w\", \"h\", \"a\", \"t\", \"e\", \"v\", \"e\", \"r\"]]\n}\n```\n\n\n\u003cdetails\u003e\u003csummary\u003e\u003cb\u003eSolution\u003c/b\u003e\u003c/summary\u003e\n\u003cp\u003e\n\n```js\nfunction transformInput(input) {\n  const sortedArray = input.array.sort((a, b) =\u003e b - a);\n\n  const extractKeys = (obj) =\u003e {\n    const keys = [];\n    for (const key in obj) {\n      keys.push(key);\n      if (typeof obj[key] === \"object\" \u0026\u0026 !Array.isArray(obj[key])) {\n        keys.push(...extractKeys(obj[key]));\n      }\n    }\n    return keys;\n  };\n\n  const objectKeys = extractKeys(input.object);\n\n  const stringArray = input.string.map((str) =\u003e str.split(\"\"));\n  return {\n    array: sortedArray,\n    object: objectKeys,\n    string : stringArray\n  };\n}\nconsole.log(transformInput(input));\n```\n\n\u003c/p\u003e\n\u003c/details\u003e\n\n#### 2. Nested Object\n Given a deeply nested object as below, return all the key values \n\n``` js\nconst nestedObject = {\n  level1: {\n    level2: {\n      level3a: {\n        level4a: \"value1\",\n        level4b: \"value2\",\n      },\n      level3b: {\n        level4c: \"value3\",\n      },\n    },\n    level2b: \"value4\",\n  },\n  level1b: \"value5\",\n};\n```\n\n\u003cdetails\u003e\u003csummary\u003e\u003cb\u003eSolution\u003c/b\u003e\u003c/summary\u003e\n\n```js\n\nconst extractKeys = (obj) =\u003e {\n  const keys = [];\n  for (const key in obj) {\n    keys.push(key);\n    if (typeof obj[key] === \"object\") {\n      keys.push(...extractKeys(obj[key]));\n    }\n  }\n  return keys;\n};\n\nconsole.log(extractKeys(nestedObject));\n\n\\\\output : [  'level1',  'level2',  'level3a', 'level4a',  'level4b', 'level3b',  'level4c', 'level2b',  'level1b']\n\n```\n\n\u003c/details\u003e\n\n\n\n#### 3. Accessing Object Properties\nGiven the following object, how would you access and print the values of the properties name and age?\n\n```js\nconst person = {\n    name: \"Alice\",\n    age: 30,\n    city: \"New York\"\n};\n```\n\n\u003cdetails\u003e\u003csummary\u003e\u003cb\u003eSolution\u003c/b\u003e\u003c/summary\u003e\n\n```js\n\nconst AccessValues = (obj) =\u003e {\n  const result = [];\n  for (const key in obj) {\n    if (key === \"name\" || key === \"age\") {\n      result.push(obj[key]);\n    }\n  }\n  return result\n};\n\nconsole.log(AccessValues(person)); //['Alice' ,30]\n\n```\n\n\u003c/details\u003e\n\n\n#### 4. Merging Objects\nYou have two objects, obj1 and obj2. Write a function that merges these two objects into one. If both objects have a property with the same key, the value from obj2 should overwrite the value from obj1.\n\n``` js\nconst obj1 = {\n  a: 1,\n  b: 2,\n  c: 3\n};\n\nconst obj2 = {\n  b: 4,\n  d: 5\n};\n```\n\n\u003cdetails\u003e\u003csummary\u003e\u003cb\u003eSolution\u003c/b\u003e\u003c/summary\u003e\n\n```js\nconst ObjectMerger = (obj1, obj2) =\u003e {\n  return {\n    ...obj1,\n    ...obj2\n  }\n}\nconsole.log(ObjectMerger(obj1,obj2)) //{ a: 1, b: 4, c: 3, d: 5 }\n\n```\n\u003c/details\u003e\n\n\n#### 5. Flattening a Nested Object\nGiven a deeply nested object, write a function that flattens it into a single-level object. The keys of the flattened object should be the paths to the nested values, joined by dots.\n\n```js\n//input\nconst nestedObject = {\n    level1: {\n        level2: {\n            level3a: {\n                level4a: \"value1\",\n                level4b: \"value2\"\n            },\n            level3b: {\n                level4c: \"value3\"\n            }\n        },\n        level2b: \"value4\"\n    },\n    level1b: \"value5\"\n};\n```\n\n```js\n//expected output\n{\n    \"level1.level2.level3a.level4a\": \"value1\",\n    \"level1.level2.level3a.level4b\": \"value2\",\n    \"level1.level2.level3b.level4c\": \"value3\",\n    \"level1.level2b\": \"value4\",\n    \"level1b\": \"value5\"\n}\n\n```\n\u003cdetails\u003e\u003csummary\u003e\u003cb\u003eSolution\u003c/b\u003e\u003c/summary\u003e\n\n```js\nconst FlattenObject = (obj, parentKey = \"\", result = {}) =\u003e {\n  for (const key in obj) {\n    if (obj.hasOwnProperty(key)) {\n      const value = obj[key];\n      const newKey = parentKey ? `${parentKey}.${key}` : key\n      if (typeof value  === \"object\"  ) {\n        FlattenObject(value, newKey,result)\n      }\n      else{\n        result[newKey] = value\n      }\n    }\n  }\n  return result;\n};\nconsole.log(FlattenObject(nestedObject));\n\n```\n\u003c/details\u003e\n\n#### 6. Merging and Summarizing Array of Objects\n\nYou have an array of objects where each object represents a sales transaction. Each transaction includes the product name, quantity sold, and the total amount. Your task is to merge these transactions and summarize the total quantity and amount for each product.\n\nInput:\n```js\nconst transactions = [\n    { product: \"Laptop\", quantity: 2, amount: 1200 },\n    { product: \"Mouse\", quantity: 5, amount: 50 },\n    { product: \"Laptop\", quantity: 1, amount: 600 },\n    { product: \"Keyboard\", quantity: 3, amount: 150 },\n    { product: \"Mouse\", quantity: 2, amount: 20 }\n];\n```\n\n\u003cdetails\u003e\u003csummary\u003e\u003cb\u003eSolution\u003c/b\u003e\u003c/summary\u003e\n\n```js\nconst Summarize = (arr) =\u003e {\n  const result = [];\n  arr.forEach((element) =\u003e {\n    const { product, quantity, amount } = element;\n\n    if (!result[product]) {\n      result[product] = { totalAmount: 0, totalQuantity: 0 };\n    }\n\n    result[product].totalAmount += amount\n    result[product].totalQuantity += quantity\n  });\n\n  return result;\n  };\n\nconsole.log(Summarize(transactions))\n/*\n      [\n        Laptop: { totalAmount: 1800, totalQuantity: 3 },\n        Mouse: { totalAmount: 70, totalQuantity: 7 },\n        Keyboard: { totalAmount: 150, totalQuantity: 3 }\n      ]\n*/\n```\n\u003c/details\u003e\n\n#### 7. Checking Object Equality\n\nYou are given two objects, and you need to write a function that checks whether they are deeply equal. Two objects are considered deeply equal if they have the same properties with the same values, including nested objects.\n\n\n\u003cdetails\u003e\u003csummary\u003e\u003cb\u003eSolution\u003c/b\u003e\u003c/summary\u003e\n\n``` js\nconst person1 = {\n  name: \"Alice\",\n  age: 30,\n  address: {\n    city: \"Wonderland\",\n    postalCode: \"12345\",\n  },\n};\n\nconst person2 = {\n  name: \"Alice\",\n  age: 30,\n  address: {\n    city: \"Wonderland\",\n    postalCode: \"12345\",\n  },\n};\n\nconst DeeplyEqual = (obj1, obj2) =\u003e {\n  if (obj1 === obj2) return true;\n  if (\n    typeof obj1 !== \"object\" ||\n    typeof obj2 !== \"object\" ||\n    obj1 === null ||\n    obj2 === null\n  ) {\n    return false;\n  }\n  const keys1 = Object.keys(obj1);\n  const keys2 = Object.keys(obj2);\n\n  if (keys1.length !== keys2.length) return false;\n\n  for (const key of keys1) {\n    if (!keys2.includes(key)) return false;\n    if (!DeeplyEqual(obj1[key], obj2[key])) return false;\n  }\n  return true;\n};\n\nconsole.log(DeeplyEqual(person1, person2));\n\n```\n\u003c/details\u003e\n\n#### 8. Counting Properties in an Object\nWrite a function that counts the number of properties (key-value pairs) in a given object. The function should return the count as an integer.\n\n```js\nconst exampleObject = {\n  name: \"Alice\",\n  age: 30,\n  profession: \"Engineer\"\n};\n```\n\n\u003cdetails\u003e\u003csummary\u003e\u003cb\u003eSolution\u003c/b\u003e\u003c/summary\u003e\n\n```js\nconst countProperties = (obj) =\u003e {\n  return Object.keys(obj).length;\n};\n\nconsole.log(countProperties(exampleObject)) //3\n```\n\n\u003c/details\u003e\n\n#### 9. Swap keys to Values in an object\n\n```js\nconst exampleObject = {\n  name: \"Alice\",\n  age: 30,\n  };\n```\n\n\u003cdetails\u003e\u003csummary\u003e\u003cb\u003eSolution\u003c/b\u003e\u003c/summary\u003e\n\n```js\nconst SwapKeysToValues = (obj) =\u003e {\n  const newObject = {};\n  for (let key in obj) {\n    if (newObject.hasOwnProperty(obj[key])) {\n      return \"Duplicate Value found\";\n    }\n    newObject[obj[key]] = key;\n  }\n  return newObject;\n};\nconsole.log(SwapKeysToValues(exampleObject)) \n```\n\n\u003c/details\u003e\n\n###  Arrays\n#### 1. Sum of min-max \n Given an array return the sum of min and max from the array\n\n``` js\nconst inputArray = [1,55,156,1,1,0,1,98]\n\n```\n\n\u003cdetails\u003e\u003csummary\u003e\u003cb\u003eSolution\u003c/b\u003e\u003c/summary\u003e\n\n```js\n\nconst SumofMinMax = (input) =\u003e {\n  if(input.length === 0){\n   return null\n  }\n  const min = Math.min(...input)\n  const max = Math.max(...input)\n\n  return min+max\n}\n\nconsole.log(SumofMinMax([])); \\\\null\nconsole.log(SumofMinMax(inputArray)); \\\\156\n\n```\n\u003c/details\u003e\n\n#### 2. Two Sum\n Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target.\n\nYou may assume that each input would have exactly one solution, and you may not use the same element twice.\n\nYou can return the answer in any order.\n\nExample :\n\nInput: nums = [2,7,11,15], target = 9\nOutput: [0,1]\nExplanation: Because nums[0] + nums[1] == 9, we return [0, 1].\n\n\u003cdetails\u003e\u003csummary\u003e\u003cb\u003eSolution\u003c/b\u003e\u003c/summary\u003e\n\n```js\n//Brute Force\nvar twoSum = function(nums, target) {\n    for(let i = 0; i \u003c nums.length; i++) {\n        for(let j = i + 1; j \u003c nums.length; j++) {\n            if (nums[i] + nums[j] == target) {\n                return [i, j]\n            }\n        }\n    }\n};\n// Hashmap- O(n)\nvar twoSum = function (nums, target) {\n    let map = {}\n    for (let i = 0; i \u003c nums.length; i++) {\n        const difference = target - nums[i]\n        if (map.hasOwnProperty(difference)) {\n            return [map[difference],i]\n        }\n        map[nums[i]] = i\n    }\n};\nconsole.log(twoSum([1,2,3,4,5],3)); \\\\[0,1]\n\n```\n\u003c/details\u003e\n\n### Strings\n#### 1. Can form Palindrome \nWrite a function CanFormPalindrome(str) that takes a single string parameter and returns \"true\" if the characters of the string can be rearranged to form a palindrome, and \"false\" otherwise. A palindrome is a word that reads the same backward as forward. Consider only alphanumeric characters and ignore case.\n\n\u003cdetails\u003e\u003csummary\u003e\u003cb\u003eSolution\u003c/b\u003e\u003c/summary\u003e\n\n``` js\nfunction CanFormPalindrome(str) {\n  const charactersCount = {};\n  for (let char of str) {\n    char = char.toLowerCase()\n    charactersCount[char] = (charactersCount[char] || 0) + 1;\n  }\n\n  let oddCount = 0;\n  for (let char in charactersCount) {\n    if (charactersCount[char] % 2 !== 0) oddCount++;\n  }\n  return oddCount \u003c= 1;\n}\n\nconsole.log(CanFormPalindrome(\"Namaste\")); //false\nconsole.log(CanFormPalindrome(\"Madam\")); //true\n\n```\n\n\u003c/details\u003e\n\n#### 2. Check if Anagram\nCreate a function AnagramCheck(str1, str2) that takes two string parameters and returns \"true\" if the two strings are anagrams of each other, and \"false\" otherwise. An anagram is a word or phrase formed by rearranging the letters of another, using all the original letters exactly once. The function should be case-insensitive and ignore spaces.\n\n\u003cdetails\u003e\u003csummary\u003e\u003cb\u003eSolution\u003c/b\u003e\u003c/summary\u003e\n\n``` js\nfunction isAnagram(str_one, str_two) {\n  const sortedStrOne = str_one.toLowerCase().split(\"\").sort().join('')\n  const sortedStrTwo = str_two.toLowerCase().split(\"\").sort().join('')\n\n  return sortedStrOne === sortedStrTwo;\n}\n\nconsole.log(isAnagram(\"Namaste\", \"World\")); //false\nconsole.log(isAnagram(\"Hero\", \"Oreh\")); //true\n```\n\n\u003c/details\u003e\n\n#### 3. Non repeating Character\nCreate a function FirstNonRepeatingCharacter(str) that takes a string and returns the first non-repeating character. If there is no such character, return an empty string.\n\n\u003cdetails\u003e\u003csummary\u003e\u003cb\u003eSolution\u003c/b\u003e\u003c/summary\u003e\n\n``` js\nfunction FirstNonRepeatingChar(str) {\n  const charCount = {};\n  for (let char of str) {\n    char = char.toLowerCase();\n    charCount[char] = (charCount[char] || 0) + 1;\n  }\n  for( let char of str){\n    if(charCount[char] === 1) return char\n  }\n  return \"\"\n}\nconsole.log(FirstNonRepeatingChar('swiss')) //w\nconsole.log(FirstNonRepeatingChar('ababababax')) //x\nconsole.log(FirstNonRepeatingChar('yolloy')) //\"\"\n\n// Alternate \nfunction RemoveDuplicatesTwo(str) {\n  let uniqueChar = new Set(str.toLowerCase());\n  return [...uniqueChar].join(\"\");\n}\n//Preserving the order and for senteces \n\nfunction RemoveDuplicatesInOrder(str) {\n  const seen = new Set();\n  const result = [];\n\n  for (let char of str.toLowerCase()) {\n    if (char.match(/[a-z]/) \u0026\u0026 !seen.has(char)) {\n      seen.add(char);\n      result.push(char);\n    } else if(char === ' ')\n    result.push(char)\n  }\n  return result.join('')\n```\n\u003c/details\u003e\n\n#### 4. Find the most repeated character \n\n\u003cdetails\u003e\u003csummary\u003e\u003cb\u003eSolution\u003c/b\u003e\u003c/summary\u003e\n\n``` js\nconst str = \"hey there welcome\";\n\nfunction MostFrequentLetter(str) {\n  let charCount = {};\n  for (let char of str) {\n    if (char !== \" \") {\n      char = char.toLowerCase();\n      charCount[char] = (charCount[char] || 0) + 1;\n    }\n  }\n  let maxCount = 1;\n  let mostFrequentChar = \"\";\n  for (let char in charCount) {\n    if (charCount[char] \u003e maxCount) {\n      maxCount = charCount[char];\n      mostFrequentChar = char;\n    }\n  }\n  return mostFrequentChar;\n}\nconsole.log(MostFrequentLetter(str));//e\n\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsrivenkat13%2Fjs-practice","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsrivenkat13%2Fjs-practice","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsrivenkat13%2Fjs-practice/lists"}