Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Object Oriented Programming Articles
Page 162 of 589
Finding mistakes in a string - JavaScript
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 MoreReplace HTML div into text element with JavaScript?
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 MoreFrequency distribution of elements - JavaScript
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 MoreIs there a DOM function which deletes all elements between two elements in JavaScript?
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 MoreStrip quotes with JavaScript to convert into JSON object?
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 MoreFind distinct elements - JavaScript
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 MoreConverting any string into camel case with JavaScript removing whitespace as well
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 MoreDetermining full house in poker - JavaScript
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 MoreMapping the letter of a string to an object of arrays - JavaScript
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 MoreFinding shared element between two strings - JavaScript
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