Web Development Articles

Page 475 of 801

Determining full house in poker - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 479 Views

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 More

Solve the Sherlock and Array problem in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 740 Views

Watson gives Sherlock an array A of length N. Then he asks him to determine if there exists an element in the array such that the sum of the elements on its left is equal to the sum of the elements on its right. We have to write this function, it should take in an array of Numbers, and any such number exists in the array, it should return its index, otherwise it should return -1. So, let's write the code for this function. Algorithm Approach The efficient approach is to: Calculate the total sum of ...

Read More

Mapping the letter of a string to an object of arrays - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 502 Views

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 More

How to force JavaScript to do math instead of putting two strings together?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 483 Views

JavaScript often concatenates when you expect addition because the + operator works differently with strings and numbers. Here are several methods to force mathematical addition instead of string concatenation. The Problem When JavaScript encounters the + operator with strings, it performs concatenation instead of addition: console.log("3" + "5"); // "35" (concatenation) console.log(3 + 5); // 8 (addition) console.log("3" + 5); // "35" (string wins, concatenation) 35 8 35 Using the Unary Plus Operator ...

Read More

Finding shared element between two strings - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 633 Views

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

Find unique and biggest string values from an array in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 295 Views

In JavaScript, we often need to filter arrays to get unique values and sort them by specific criteria. This article demonstrates how to find the longest unique string values from an array of objects. Problem Statement Given an array of objects with text properties, we need to create a function that returns n objects with the longest unique string values. If fewer than n unique objects exist, return all unique objects. const arr = [ {text: 'use'}, {text: 'secur'}, {text: 'form'}, ...

Read More

Finding letter distance in strings - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 779 Views

We are required to write a JavaScript function that takes in a string as first argument and two single element strings. The function should return the distance between those single letter strings in the string taken as first argument. For example − If the three strings are − const str = 'Disaster management'; const a = 'i', b = 't'; Then the output should be 4 because the distance between 'i' and 't' is 4 Understanding Letter Distance Letter distance is the absolute difference between the index positions of two characters in ...

Read More

Check whether a number is a Fibonacci number or not JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 525 Views

We are required to write a JavaScript function that takes in a number and returns a boolean based on the fact whether or not it comes in the fibonacci series. For example − If the function call is like this − fibonacci(12); fibonacci(89); fibonacci(55); fibonacci(534); Then the output should be − false true true false What is the Fibonacci Series? The Fibonacci series is a sequence where each number is the sum of the two preceding ones: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...

Read More

Function that parses number embedded in strings - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 184 Views

JavaScript's built-in functions like parseInt() and parseFloat() only parse numbers from the beginning of a string, stopping when they encounter non-numeric characters. When you need to extract all digits from anywhere within a string, you need a custom solution. The Problem with Built-in Methods Standard parsing methods fail with embedded numbers: console.log(parseInt('454ffdg54hg53')); // 454 (stops at first non-digit) console.log(parseFloat('12.34abc56.78')); // 12.34 (stops at 'a') 454 12.34 Method 1: Loop Through Characters This approach iterates through each character, extracting only digits: const numStr = '454ffdg54hg53'; ...

Read More

Finding next n leap years in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 636 Views

We are required to write a function that takes a positive integer n and returns an array of next n leap years. We will break this problem into three parts: Part 1: Finding Current Year via JavaScript The code to find current year via JavaScript will be: // getting the current year from a new instance of Date object const year = new Date().getFullYear(); console.log("Current year:", year); Current year: 2024 Part 2: Checking for Leap Year We will now write a function isLeap() that takes in a number and returns ...

Read More
Showing 4741–4750 of 8,010 articles
« Prev 1 473 474 475 476 477 801 Next »
Advertisements