Object Oriented Programming Articles

Page 162 of 589

Finding mistakes in a string - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 542 Views

We are required to write a JavaScript function that takes in two strings. The first string is some mistyped string and the second string is the correct version of this string. We can assume that the two strings we are getting as argument will always have the same length. We have to return the number of mistakes that exist in the first string by comparing it character by character with the correct version. Approach The solution involves iterating through both strings simultaneously and comparing each character at the same position. When characters don't match, we increment our ...

Read More

Replace HTML div into text element with JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 889 Views

To replace HTML div content with text from another element, you can use document.querySelectorAll() to select multiple elements and getElementsByClassName() to get the source text. This approach allows you to dynamically update multiple elements with content from a single source. Example Replace Div Content My Name is John ...

Read More

Frequency distribution of elements - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 1K+ Views

We are required to write a JavaScript function that takes a string and returns an object representing the frequency distribution of each character in the string. const str = 'This string will be used to calculate frequency distribution'; We need to return an object that represents the frequency distribution of various characters present in the string. Example Following is the code: const str = 'This string will be used to calculate frequency distribution'; const frequencyDistribution = str => { const map = {}; ...

Read More

Is there a DOM function which deletes all elements between two elements in JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 371 Views

In JavaScript, there's no single DOM function that directly deletes all elements between two elements. However, you can achieve this using a combination of DOM methods like nextElementSibling and remove(). Problem Setup Consider the following HTML structure where we want to remove all elements between a starting point and an ending point: START My Name is John My Name is David My Name is Bob My Name is Mike My Name is Carol END We need to remove all elements between the (START) and (END) elements. Solution Using nextElementSibling ...

Read More

Strip quotes with JavaScript to convert into JSON object?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 2K+ Views

When dealing with JSON strings that have escaped quotes or extra quote wrapping, you need to clean them before parsing. This article shows how to strip unwanted quotes and convert the cleaned string into a JavaScript object. The Problem Sometimes JSON data comes with extra quotes or escaped quote characters that prevent direct parsing. Here's a common scenario where a JSON string is wrapped in extra quotes and has doubled internal quotes: var studentDetails = `"{""name"": ""John"", ""subjectName"": ""Introduction To JavaScript""}"`; console.log("Original string:"); console.log(studentDetails); Original string: "{""name"": ""John"", ""subjectName"": ""Introduction To JavaScript""}" ...

Read More

Find distinct elements - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 186 Views

We are required to write a JavaScript function that takes in an array of literals, such that some array elements are repeated. We are required to return an array that contains elements that appear only once (not repeated). For example: If the array is: const arr = [9, 5, 6, 8, 7, 7, 1, 1, 1, 1, 1, 9, 8]; Then the output should be: const output = [5, 6]; Using indexOf() and lastIndexOf() The first approach uses indexOf() and lastIndexOf() to check if an element appears only once. If ...

Read More

Converting any string into camel case with JavaScript removing whitespace as well

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 1K+ Views

In JavaScript, camel case formatting converts strings by lowercasing the first letter and capitalizing the first letter of each subsequent word, while removing all whitespace. What is Camel Case? Camel case is a naming convention where the first word starts with a lowercase letter and each subsequent word starts with an uppercase letter, with no spaces or punctuation. For example: "hello world" becomes "helloWorld". Using Regular Expression Method The most efficient approach uses a regular expression with the replace() method to transform the string: function convertStringToCamelCase(sentence) { return sentence.replace(/(?:^\w|[A-Z]|\b\w|\s+)/g, ...

Read More

Determining full house in poker - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 479 Views

A "full house" in poker is a hand containing three cards of one rank and two cards of another rank (e.g., three Kings and two Aces). We need to write a JavaScript function that checks if an array of five cards represents a full house. Understanding Full House A full house requires exactly: Three cards of the same rank (three of a kind) Two cards of another same rank (a pair) Example Implementation Here's a JavaScript function to detect a full house: const arr1 = ['K', 'K', 'K', 'A', 'A']; // Full ...

Read More

Mapping the letter of a string to an object of arrays - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 503 Views

Given a string, we are required to write a function that creates an object that stores the indexes of each letter in an array. The letters (elements) of the string must be the keys of object. The indexes should be stored in an array and those arrays are values. For example, if the input string is: const str = 'cannot'; Then the output should be: const output = { 'c': [0], 'a': [1], 'n': [2, 3], ...

Read More

Finding shared element between two strings - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 633 Views

We are required to write a JavaScript function that takes in two strings that may / may not contain some common elements. The function should return an empty string if no common element exists otherwise a string containing all common elements between two strings. Following are our two strings − const str1 = 'Hey There!!, how are you'; const str2 = 'Can this be a special string'; Example Following is the code − const str1 = 'Hey There!!, how are you'; const str2 = 'Can this be a special string'; const ...

Read More
Showing 1611–1620 of 5,881 articles
« Prev 1 160 161 162 163 164 589 Next »
Advertisements