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 on Trending Technologies
Technical articles with clear explanations and examples
How to determine if date is weekend in JavaScript?
In JavaScript, you can determine if a date falls on a weekend by using the getDay() method. This method returns 0 for Sunday and 6 for Saturday, making weekend detection straightforward. How getDay() Works The getDay() method returns a number representing the day of the week: 0 = Sunday 1 = Monday 2 = Tuesday 3 = Wednesday 4 = Thursday 5 = Friday 6 = Saturday Basic Weekend Check Here's how to check if a specific date is a weekend: var givenDate = new Date("2020-07-18"); var currentDay = givenDate.getDay(); var ...
Read MoreThe Zombie Apocalypse case study - JavaScript
A nasty zombie virus is spreading out in the digital cities. We work at the digital CDC and our job is to look over the city maps and tell which areas are contaminated by the zombie virus so the digital army would know where to drop the bombs. They are the new kind of digital zombies which can travel only in vertical and horizontal directions and infect only numbers same as them. We'll be given a two-dimensional array with numbers in it. For some mysterious reason patient zero is always found in north west area of the ...
Read MoreHow to convert PHP array to JavaScript array?
Converting PHP arrays to JavaScript arrays is essential when passing server-side data to client-side scripts. PHP's json_encode() function provides a reliable way to achieve this conversion for both single and multidimensional arrays. Syntax // PHP side $phpArray = array('value1', 'value2'); // JavaScript side var jsArray = ; Example: Converting Simple PHP Array Let's start with a simple PHP array containing user information: var arr = ; console.log(arr); // Output: ["Amit", "amit@example.com"] console.log(arr[0]); // Output: "Amit" ...
Read MorePrim's algorithm in Javascript
Prim's algorithm is a greedy algorithm that finds a minimum spanning tree for a weighted undirected graph. It finds a subset of the edges that forms a tree that includes every vertex, where the total weight of all the edges in the tree is minimized. The algorithm operates by building this tree one vertex at a time, from an arbitrary starting vertex, at each step adding the cheapest possible connection from the tree to another vertex. How Prim's Algorithm Works Let us look at an illustration of how Prim's algorithm works: 1. Choose any arbitrary node ...
Read MoreHow to toggle text with JavaScript?
Text toggling allows you to dynamically change displayed content based on user interaction. This is commonly used for showing/hiding information, switching between states, or creating interactive UI elements. Basic Toggle Example Here's a complete example that toggles between two text values when a button is clicked: body { font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif; ...
Read MoreHow to count a depth level of nested JavaScript objects?
We have an array of objects, which further have nested objects like this − const arr = [{ id: 0, children: [] }, { id: 1, children: [{ id: 2, children: [] }, { id: 3, children: [{ id: 4, children: [] }] }] }]; ...
Read MoreHow can I get seconds since epoch in JavaScript?
To get seconds since epoch in JavaScript, you can use Date.getTime() which returns milliseconds since January 1, 1970 (Unix epoch), then divide by 1000 to convert to seconds. Syntax var date = new Date(); var epochSeconds = Math.round(date.getTime() / 1000); Method 1: Using Math.round() The most common approach uses Math.round() to handle decimal precision: var currentDate = new Date(); var epochSeconds = Math.round(currentDate.getTime() / 1000); console.log("Seconds since epoch:", epochSeconds); console.log("Type:", typeof epochSeconds); Seconds since epoch: 1594821507 Type: number Method 2: Using Math.floor() For exact truncation without ...
Read MoreIf ([] == false) is true, why does ([] || true) result in []? - JavaScript
If we look closely at the problem statement, the difference between ([] == false) and ([] || true) is the following − In the first case, we are using loose conditional checking, allowing type coercion to take over. While in the second case, we are evaluating [] to its respective Boolean (truthy or falsy) which makes use of the function Boolean() instead of type coercion under the hood. Let's now unveil the conversions that happen behind the scenes in both cases. Case 1 - ([] == false) According to the MDN docs, when two data ...
Read MoreValidating email and password - JavaScript
Suppose, we have this dummy array that contains the login info of two of many users of a social networking platform − const array = [{ email: 'usman@gmail.com', password: '123' }, { email: 'ali@gmail.com', password: '123' }]; We are required to write a JavaScript function that takes in an email string and a password string. The function should return a boolean based on the fact whether or not the user exists in the database. Example Following is the code − ...
Read MoreWith JavaScript how can I find the name of the web browser, with version?
To find the name of the web browser with version in JavaScript, you can use the navigator object which provides information about the user's browser and system. Basic Browser Detection The traditional approach uses navigator.userAgent to detect browser types: Browser Detection Example var userAgent = navigator.userAgent; var opera = (userAgent.indexOf('Opera') != -1); var ie = (userAgent.indexOf('MSIE') != -1 || ...
Read More