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
Articles by AmitDiwan
Page 405 of 840
Corner digit number difference - JavaScript
We are required to write a JavaScript function that takes in a number, constructs a new number from the first and last digit of that number and returns the difference between the original number and the number thus formed. For example: If the input is 34567 Then the corner digits number will be: 37 And the output will be: 34530 Algorithm The solution involves extracting the first and last digits, combining them to form a corner number, then calculating the difference. Example Following is the code: ...
Read MoreReturning the highest number from object properties value – JavaScript
When working with JavaScript objects, you might need to find the property with the highest value. For example, finding the highest rating from a property rating system. Suppose we have an object that contains ratings of a property over some criteria like this: const rating = { "overall": 92, "atmosphere": 93, "cleanliness": 94, "facilities": 89, "staff": 94, "security": 92, "location": 88, "valueForMoney": 92 } We need to write a JavaScript function ...
Read MoreJavaScript Let
The JavaScript let keyword, introduced in ES6 (2015), allows us to declare block-scoped variables. Unlike var, variables declared with let are only accessible within the block where they are defined. Block Scope with let Variables declared with let are confined to their block scope and cannot be accessed outside of it. JavaScript Let Example JavaScript Let Block Scope Test Block Scope ...
Read MoreHow to get the child element of a parent using JavaScript?
JavaScript provides several methods to access child elements of a parent element. The most common approaches include using children, childNodes, querySelector, and querySelectorAll. Using the children Property The children property returns a live HTMLCollection of all direct child elements, excluding text nodes and comments. Getting Child Elements body { font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif; } .child1, .child2, .child3 { display: none; ...
Read MoreRecursively flat an object JavaScript
We are required to write a function that flattens nested objects by converting deeply nested properties into dot-notation keys. This is useful for data transformation and API processing. If the input object is: const input = { a: 0, b: {x: {y: 1, z: 2}}, c: 3 }; Then the output of the function should be: const output = { a: 0, 'b.x.y': 1, 'b.x.z': 2, c: 3 } Recursive ...
Read MoreReturn TRUE if the first string starts with a specific second string JavaScript
We are required to write a JavaScript function that takes in two strings and checks whether the first string starts with the second string or not. For example: If the two strings are: "Disaster management report" "Disas" Then our function should return true There are multiple ways to check if a string starts with another string in JavaScript. Let's explore the most common approaches. Using startsWith() Method (Recommended) The startsWith() method is the built-in and most straightforward way to check if a string starts with another string: const first = 'The ...
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 MoreJavaScript Location protocol Property
The location.protocol property in JavaScript returns the protocol scheme of the current URL, including the colon (:). This property is useful for determining whether a page is loaded over HTTP, HTTPS, or other protocols. Syntax location.protocol Return Value Returns a string representing the protocol scheme of the URL, including the trailing colon. Common values include: "https:" - Secure HTTP protocol "http:" - Standard HTTP protocol "file:" - Local file protocol "ftp:" - File Transfer Protocol Example ...
Read MoreCan I verify if a JavaScript variable is loaded if so, how?
To verify if a JavaScript variable has been loaded or initialized, you can check if it is undefined or has a null value. JavaScript provides several methods to perform this check effectively. The most common approaches include direct comparison with undefined and null, using the typeof operator, or checking if the variable exists in a specific scope. Method 1: Using Direct Comparison This method checks if a variable is undefined or null using strict equality: Variable Check Example ...
Read MoreMerge object properties through unique field then print data - JavaScript
Let's say we have a students object containing two properties names and marks. The names is an array of objects with each object having two properties name and roll, similarly marks is an array of objects with each object having properties mark and roll. Our task is to combine the marks and names properties according to the appropriate roll property of each object. Problem Statement The students object is given here: const students = { marks: [{ roll: 123, ...
Read More