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
Web Development Articles
Page 418 of 801
JavaScript program to merge two objects into a single object and adds the values for same keys
We have to write a function that takes in two objects, merges them into a single object, and adds the values for same keys. This has to be done in linear time and constant space, means using at most only one loop and merging the properties in the pre-existing objects and not creating any new variable. So, let's write the code for this function − Example const obj1 = { value1: 45, value2: 33, value3: 41, value4: 4, ...
Read MoreSum of consecutive numbers in JavaScript
Let's say, we have to write a function that takes in an array and returns another array in which the consecutive similar numbers are added up together. For example − const array = [1, 5, 5, 5, 8, 8, 9, 1, 4, 4, 2]; console.log("Original array:", array); Original array: [ 1, 5, 5, 5, 8, 8, 9, 1, 4, 4, 2 ] The output should be − [1, 15, 16, 9, 1, 8, 2] All consecutive 5s added up to 15, then 2 consecutive 8s added up to ...
Read MoreGet the longest and shortest string in an array JavaScript
We have an array of string literals like this: const arr = ['Some', 'random', 'words', 'that', 'actually', 'form', 'a', 'sentence.']; We need to write a function that returns the longest and shortest word from this array. We will use the Array.prototype.reduce() method to keep track of the longest and shortest word during iteration. Using Array.reduce() Method The reduce() method processes each element and maintains an accumulator object containing both the longest and shortest strings found so far: const arr = ['Some', 'random', 'words', 'that', 'actually', 'form', 'a', 'sentence.']; const findWords = ...
Read MoreDifference between Procedural and Declarative Knowledge
We can express the knowledge in various forms to the inference engine in the computer system to solve the problems. There are two important representations of knowledge namely, procedural knowledge and declarative knowledge. The basic difference between procedural and declarative knowledge is that procedural knowledge gives the control information along with the knowledge, whereas declarative knowledge just provides the knowledge but not the control information to implement the knowledge. Read through this article to find out more about procedural knowledge and declarative knowledge and how they are different from each ...
Read MoreJavaScript code for recursive Fibonacci series
We have to write a recursive function fibonacci() that takes in a number n and returns an array with first n elements of fibonacci series. The Fibonacci sequence starts with 0 and 1, where each subsequent number is the sum of the two preceding ones. Understanding the Fibonacci Sequence The Fibonacci sequence follows this pattern: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34... where F(n) = F(n-1) + F(n-2). Recursive Implementation Here's a recursive approach that builds the Fibonacci array: const fibonacci = (n, res = [], count = 1, last = ...
Read MoreHow to trigger a button click on keyboard "enter" with JavaScript?
To trigger a button click when the user presses the "Enter" key, you can use JavaScript's event listeners to detect the keypress and programmatically trigger the button's click event. Basic Example Here's a complete example that triggers a button click when Enter is pressed inside an input field: Trigger Button Click on Enter Trigger Button Click on Enter Example Click ...
Read MoreHow to toggle between password visibility with JavaScript?
To toggle between password visibility with JavaScript, you can change the input type between "password" and "text" using a checkbox or button. This allows users to see their password when needed. How It Works The toggle functionality works by: Adding an event listener to a checkbox or button Checking the current input type Switching between "password" and "text" types Example: Using Checkbox Toggle Password Toggle Example Password Visibility Toggle ...
Read MoreHow to create an Autocomplete with JavaScript?
Creating an autocomplete feature enhances user experience by providing real-time suggestions as users type. This implementation uses vanilla JavaScript to filter and display matching results from a predefined array. HTML Structure The autocomplete requires a wrapper div with the autocomplete class and an input field: Complete Example * { box-sizing: border-box; ...
Read MoreHow to add form validation for empty input fields with JavaScript?
Form validation ensures users provide required information before submitting. JavaScript allows client-side validation to check for empty input fields and provide immediate feedback. Basic Empty Field Validation Here's a complete example that validates empty input fields: Form Validation Example JavaScript Empty Input Field Validation Name: ...
Read MoreHow to create a filter list with JavaScript?
JavaScript filter lists allow users to search and filter through data dynamically. This is commonly used for search functionality in websites and applications. Complete HTML Example * { box-sizing: border-box; } .searchInput { width: 100%; font-size: 16px; ...
Read More