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 436 of 840
JavaScript 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 MoreDisplay in console if Select list value contains a value from an array in JavaScript?
In JavaScript, you can check if a selected dropdown value exists in an array using event listeners and array methods. This is useful for validation or conditional logic based on user selections. HTML Structure Let's start with a basic dropdown containing several names: John David Chris Mike Bob Carol Array to Check Against We'll define an array of names to compare with the selected value: ...
Read MoreMerge two arrays with alternating Values in JavaScript
Merging two arrays with alternating values means creating a new array that picks elements from each array in turn: first element from array1, first element from array2, second element from array1, and so on. Using While Loop with Index Tracking This approach uses separate counters to track positions in each array and alternates between them: const arr1 = [34, 21, 2, 56, 17]; const arr2 = [12, 86, 1, 54, 28]; let run = 0, first = 0, second = 0; const newArr = []; while(run < arr1.length + arr2.length){ if(first ...
Read MoreRecursive product of summed digits JavaScript
We need to create a function that takes multiple numbers as arguments, adds them together, then repeatedly multiplies the digits of the result until we get a single digit. Problem Breakdown For example, with arguments 16, 34, 42: 16 + 34 + 42 = 92 Then multiply digits until single digit: 9 × 2 = 18 1 × 8 = 8 (final result) Solution Approach We'll break this into two helper functions: produce() - calculates the product of digits in a number using ...
Read MoreHow do I check whether a checkbox is checked in jQuery?
To check whether a checkbox is checked in jQuery, you can use several methods. The most common approaches are using the .is(':checked') method, the .prop('checked') method, or implementing toggle functionality. Method 1: Using .is(':checked') The .is(':checked') method returns a boolean value indicating whether the checkbox is checked: Checkbox Check Example Check me Check Status ...
Read MoreHow to invoke a function as a function and method?
In JavaScript, functions can be invoked in different ways. When called directly, it's function invocation. When called as a property of an object, it's method invocation. Function vs Method Invocation Function invocation: Calling a function directly by its name. Method invocation: Calling a function that belongs to an object using dot notation. Example Function and Method Invocation body { ...
Read MoreDot notation in JavaScript
Dot notation is the most common way to access object properties in JavaScript. It uses a dot (.) between the object name and property name to retrieve or set values. Syntax objectName.propertyName Basic Example Dot Notation Example Student Information Show Student Details function Student(name, age, standard) { ...
Read MoreHighest and lowest in an array JavaScript
In JavaScript, finding the difference between the highest and lowest values in an array is a common task. This article demonstrates multiple approaches to calculate this difference efficiently. Using Math.max() and Math.min() with Spread Operator The most straightforward approach uses the spread operator with Math.max() and Math.min(): const arr = [23, 54, 65, 76, 87, 87, 431, -6, 22, 4, -454]; const arrayDifference = (arr) => { const max = Math.max(...arr); const min = Math.min(...arr); return max - min; }; console.log("Array:", arr); ...
Read MoreWrite an algorithm that takes an array and moves all of the zeros to the end JavaScript
We need to write a function that moves all zeros in an array to the end while maintaining the order of non-zero elements. This algorithm should work in-place without using extra space. Problem Overview Given an array with mixed numbers including zeros, we want to push all zeros to the end while keeping other elements in their relative order. For example, [1, 0, 3, 0, 5] should become [1, 3, 5, 0, 0]. Method 1: Using splice() and push() This approach removes zeros when found and adds them to the end: const arr = ...
Read MoreHow can I declare and define multiple variables in one statement with JavaScript?
In JavaScript, you can declare and define multiple variables in a single statement by separating them with commas. This approach works with var, let, and const keywords. Syntax var variable1 = value1, variable2 = value2, variable3 = value3; // Or with let/const let variable1 = value1, variable2 = value2, variable3 = value3; Example: Using var var firstName = "My First Name is David", lastName = "My Last Name is Miller", ...
Read More